Set/Get attribute syntatic sugar

Elmo Mäntynen elmo13 at
Wed Jun 29 02:07:14 CEST 2005

Hash: SHA1

Peter Hansen 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?
> Perhaps not, but now that you've pointed it out they've taken the time
> machine back and fixed the problem before it arose:

Maybe funny, but a bit too cocky for my taste. Robert kern is propably
right about what he really meant so don't be too hasty in the future,
right?). Looking at his code example I got the picture that he's of the
kind that could come up with something useful. So either he's
right(which I think is the case), or it's just the kind of silly mistake
all of us sometimes make. I sure think some one should look in to this

>>>> class C:
> ...   def __setattr__(self, e, v):
> ...     print 'setting %s to %s' % (e, v)
> ...     self.__dict__[e] = v
> ...
>>>> o = C()
>>>> v = 'mystring'
>>>> o.e = v
> setting e to mystring
>>>> o.e
> 'mystring'
> -Peter
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Thunderbird -


More information about the Python-list mailing list