Set/Get attribute syntatic sugar

Elmo Mäntynen elmo13 at jippii.fi
Tue Jun 28 20:07:14 EDT 2005


-----BEGIN PGP SIGNED MESSAGE-----
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
suggestion.

>>>> 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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFCweYyctNFyQJObrsRAhGtAJwJXlhQ9i1PIQKj1fus6GIq7mfDVgCeJBRw
vq6yJrozRTUSTu+p8akVbVw=
=k4EW
-----END PGP SIGNATURE-----



More information about the Python-list mailing list