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

Larry Hastings larry at hastings.org
Mon Feb 12 16:33:44 CET 2007


Ben North wrote:
> Jack Jansen:
>   
>> To me self.[method_name] = self.metadata.[method_name] looks better:
>> what we're doing here is more like dictionary lookup than calling
>> functions.
>>     
> In the same way, though, would this be viewed as too similar to normal
> dictionary/list indexing?
>   
I think that's its strength; it's pleasantly symmetric to using [] on a 
dict:

    dict    attribute     behavior
    --------------------------------
    []      .[]           pulls out named thing, throws exception
                          on failure, no default, is an lvalue

    .get()  .getattr()    pulls out named thing, returns default
                          value on error (specifiable as second
                          argument), not an lvalue

List comprehensions look like funny lists, generator expressions look 
like funny expressions, I see no reason why attribute dereferencing 
shouldn't look like funny dict dereferencing.

+1.

> What would the reaction be to
>    obj.{member}
> instead?  Braces are already used to construct dictionaries, so this has
> some of the right connotations.
-0.5.  You're not constructing anything, you're looking up something.

For the record, I'm also -1 on the two-argument form--this isn't a 
function call.


/larry/


More information about the Python-Dev mailing list