Antoine Pitrou wrote:
On Mon, 30 Jan 2012 12:49:54 -0600 Massimo Di Pierro
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 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.
Sure you do -- as soon as 'something' can be passed in via a variable: def some_func(name): print(d.name) # uh, no print(d[name]) # okay, this works And when you are working with known objects (not passed in names): def some_other_func(): flam(d.something) sniggle(d.somethingelse) Okay, *need* might be too strong, as you could get by with [] access -- but . access is so much nicer when possible (saves three characters, which can make a difference for those of us with wimpy wrists!). ~Ethan~