[Python-Dev] New syntax for 'dynamic' attribute access

M.-A. Lemburg mal at egenix.com
Mon Feb 12 16:43:58 CET 2007


On 2007-02-12 16:19, Georg Brandl wrote:
>> Tim Delaney asked in particular:
>>> Have you checked if [the existing uses of getattr, where "getattr" in
>>> that scope is a function argument with default value the built-in
>>> "getattr"] are intended to bring the "getattr" name into local scope
>>> for fast lookup, or to force a binding to the builtin "gettattr" at
>>> compile time (two common (ab)uses of default arguments)?  If they are,
>>> they would be better served by the new syntax.
>> They're all in Lib/codecs.py, and are of the form:
>>
>>     class StreamRecoder:
>>         def __getattr__(self, name,
>>                         getattr=getattr):
>>
>>             """ Inherit all other methods from the underlying stream.
>>             """
>>             return getattr(self.stream, name)
>>
>> Without digging deeper into that code I'm afraid I can't say precisely
>> what is going on.
> 
> Since that is a special method and ought to have the signature
> __getattr__(self, name), I think it's safe to assume that that's meant
> as an optimization.

I can confirm that: it's a case of fast-local-lookup optimization.

You can add a -1 from me to the list as well: I don't think that
dynamic lookups are common enough to warrant new syntax.

Even if you do add a new syntax for this, using parenthesis is
a poor choice IMHO as the resulting code looks too much like a
function call (e.g. "callable.(variable)").

Other choices would be square brackets [], but these have the
same problem as they are in use for indexing.

The only brackets that are not yet overloaded in the context
of applying them to an object are curly brackets, so
"callable.{variable}" would cause enough raising eyebrows
to not think of a typo.

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Feb 12 2007)
>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________

:::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! ::::


More information about the Python-Dev mailing list