dictionary and __getattr__

Emile van Sebille emile at fenx.com
Thu Sep 6 07:22:23 EDT 2001


You could try:

>>> class myDict(UserDict):
...     def __getattr__(self, attr):
...             return self.data[attr]
...
>>> d = myDict()
>>> d['a'] = 'this is a'
>>> d.a
'this is a'

HTH,

--

Emile van Sebille
emile at fenx.com

---------
"Harald Kirsch" <kirschh at lionbioscience.com> wrote in message
news:yv28zfsr6e8.fsf at lionsp093.lion-ag.de...


Wouldn't it be nice if this would work:

  d = {'x': 1}
  print d.x

i.e. every entry in a dictionary is also an attribute of the
dictionary itself.

Is there a class wrapper for that somewhere available?

  Harald Kirsch

--
----------------+------------------------------------------------------
Harald Kirsch   | kirschh at lionbioscience.com | "How old is the epsilon?"
LION bioscience | +49 6221 4038 172          |        -- Paul Erdös
       *** Please do not send me copies of your posts. ***




More information about the Python-list mailing list