Set/Get attribute syntatic sugar

Michael Hoffman cam.ac.uk at mh391.invalid
Tue Jun 28 16:01:20 EDT 2005


Robert Kern wrote:
>> Заур Шибзухов wrote:
>>
>>> There is a syntactic sugar for item access in
>>> dictionaries and sequences:
>>>
>>> o[e] = v <-> o.__setitem__(e, v)
>>> o[e] <-> o.__getitem__(e)
>>>
>>> where e is an expression.
>>>
>>> There is no similar way for set/get attribute for objects.
>>> If e is a given name, then     o.e = v <-> o.__setattr__(e, v)
>>> o.e <-> o.__getattr__(e)
>>>
>>> Anybody thought about this issue?
> 
> I think he means something like this:
> e = 'i_am_an_attribute'
> o.(e) = 10
> o.i_am_an_attribute == 10

I always use the getattr() and setattr() built-ins which could be 
considered syntactic sugar when compared to the alternatives above.

But that's all the syntactic sugar you need--any more will give you 
cancer of the semicolon.
-- 
Michael Hoffman



More information about the Python-list mailing list