map -> class instance

Tim Peters at
Thu Jun 13 00:46:01 CEST 2002

[John Hunter]
> I want to convert dictionary instances to instances of a class that
> has the keys of the map as attributes. ...

Hmm.  Nobody gave the obvious <wink> way yet:

class Map2Class:
    def __init__(self, amap):

That's it.  Then

>>> m = {'first': 'John',
...      'last': 'Hunter',
...      'age': 34}
>>> c = Map2Class(m)
>>> print c.first, c.last, c.age
John Hunter 34

at-34-life-can-be-unclear-ly y'rs  - tim

More information about the Python-list mailing list