namespace dictionaries ok?
Alex Martelli
aleaxit at yahoo.com
Wed Oct 26 10:42:19 EDT 2005
Ron Adam <rrr at ronadam.com> wrote:
...
> class namespace(dict):
> def __getattr__(self, name):
> return self.__getitem__(name)
...
> Any thoughts? Any better way to do this?
If any of the keys (which become attributes through this trick) is named
'update', 'keys', 'get' (and so on), you're toast; it really looks like
a nasty, hard-to-find bug just waiting to happen. If you're really
adamant on going this perilous way, you might try overriding
__getattribute__ rather than __getattr__ (the latter is called only when
an attribute is not found "in the normal way").
If you think about it, you're asking for incompatible things: by saying
that a namespace X IS-A dict, you imply that X.update (&c) is a bound
method of X; at the same time, you also want X.update to mean just the
same thing as X['update']. Something's gotta give...!-)
Alex
More information about the Python-list
mailing list