map -> class instance
Chris Liechti
cliechti at gmx.net
Wed Jun 12 18:24:05 EDT 2002
John Hunter <jdhunter at nitace.bsd.uchicago.edu> wrote in
news:m2r8jcp3pv.fsf at video.paradise.lost:
> I want to convert dictionary instances to instances of a class that
> has the keys of the map as attributes. I naively tried this:
>
> class Map2Class:
> def __init__(self,m):
self.__dict__['m'] = m
> def __getattr__(self, key):
> return self.m[key]
>
> def __setattr__(self, key, val):
> self.m[key] = val
>
> def __delattr__(self, key):
> if self.m.has_key(key):
> del self.m[key]
see above... you have to avoid __setattr__ for your attributes and __dict__
is found witouth calling __gettattr__ thus the above works.
chris
--
Chris <cliechti at gmx.net>
More information about the Python-list
mailing list