[Python-ideas] Dict-like object with property access
Antoine Pitrou
solipsis at pitrou.net
Mon Jan 30 20:02:26 CET 2012
On Mon, 30 Jan 2012 12:49:54 -0600
Massimo Di Pierro
<massimo.dipierro at gmail.com> wrote:
> Trying to make sure I understand where we disagree and perhaps explain my problem better. For me this has very little to do with dictionaries.
>
> STEP 1) Today we can do this:
>
> class Dummy(object): pass
>
> d = Dummy()
> d.something = 5
> print d.something
>
> Is anybody calling this un-pythonic?
Depends what you're doing with it, but having a custom class which
serves as nothing but a plain container is quite contrived in my
opinion.
> STEP 2) We can now overload the [] to make the dynamic attributes accessible in an alternative syntax:
>
> class Dummy(object):
> def __getitem__(self,key): return getattr(self,key)
> def __setitem__(self,key,value): return setattr(self,key,value)
> d = Dummy()
> d.something = 5
> d['something'] = 5
> print d.something
> print d['something']
>
> STEP 3) Is anybody calling this un-pythonic?
Yes. You don't need both kinds of accesses.
Regards
Antoine.
More information about the Python-ideas
mailing list