class attrdict

Hallvard B Furuseth h.b.furuseth at usit.uio.no
Fri Mar 9 04:38:29 EST 2007


Alex Martelli writes:
> You make a good point.  I do like being able to say foo.bar=baz rather
> than foo['bar']=baz in certain cases -- not so much to save 3 chars, but
> to avoid excessive punctuation; however, I don't really need this AND
> all of dict's power at the same time, so, I don't inherit from dict:-).

Yes.  Attribute syntax looks nicer, in particular one implements a sort
of private "variables collected in a dict" thing (e.g. SQL field names)
but still wants some dict functionality.

Another variant I thought of would be to prefix dict methods with '_'
(except those that already start with '__') and (if implemented as a
dict subtype) also override the original names with a "sorry, use
_<foo>" error method.

(Posting a bit sporatically currently, disappearing for a week again
now.)

-- 
Hallvard



More information about the Python-list mailing list