Set/Get attribute syntatic sugar
Gary Wilson Jr
gdub at ece.utexas.edu
Tue Jun 28 18:05:20 EDT 2005
Заур Шибзухов 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?
How about inheriting the dict class, something like this...
>>> class C(dict):
... pass
...
>>> e = 'myAttribute'
>>> v = 'syntactic sugar'
>>> o = C()
>>> o[e] = v
>>> o[e]
'syntactic sugar'
More information about the Python-list
mailing list