__getattr__, __setattr__ and pickle
mwojc
mwojc at NOSPAMp.lodz.pl
Wed Aug 13 06:31:21 EDT 2008
Bruno Desthuilliers wrote:
> >>> class Prop(object):
> ... @apply
> ... def prop():
> ... def fget(self): return self._prop
> ... def fset(self, val): self._prop = val
> ... return property(**locals())
> ... def __init__(self, val): self.prop=val
> ...
> >>> class Hook(object):
> ... def __getattr__(self, name):
> ... if name == 'prop':
> ... return self._prop
> ... raise AttributeError("yadda")
> ... def __setattr__(self, name, val):
> ... if name == 'prop':
> ... self.__dict__['_prop'] = val
> ... else:
> ... # XXX : INCORRECT IMPLEMENTATION, DONT DO THIS !
> ... self.__dict__[name] = val
> ... # correct implementation:
> ... # super(Hook, self).__setattr__(name, value)
> ... def __init__(self, val): self.prop=val
Hi!
Finally i ended up with all the suggestions you gave me. The speed is
important to me besause i get/set my attribute a lot... The only doubts i
have are with the @apply decorator, because 'apply' seems to be depreciated
since python 2.3... And another thing: why writing for all
attributes 'self.__dict__[name] = val' in __setattr__ is incorrect?
Thanks a lot!
Greetings,
--
Marek
More information about the Python-list
mailing list