Getters and Setters
Neil Schemenauer
nascheme at ucalgary.ca
Thu Jul 15 04:58:59 EDT 1999
More information about the Python-list mailing list
Thu Jul 15 04:58:59 EDT 1999
- Previous message (by thread): Getters and Setters
- Next message (by thread): Getters and Setters
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Andrew Dalke <dalke at bioreason.com> wrote: >Here's one optimization, don't recreate getter/setter objects. If you don't recreate the objects you end of with reference loops. I don't see any way around this. There is another problem with your optimizations. What happens if the value of the attribute changes? My second version had this problem too. Also, what if someone holds a reference to the method and calls it periodicly? This is the best I can do: class _Getter: def __init__(self, dict, name): self.dict = dict self.name = name def __call__(self): return self.dict[self.name] class _Setter: def __init__(self, dict, name): self.dict = dict self.name = name def __call__(self, value): self.dict.__dict__[self.name] = value class GetterSetter: def __getattr__(self, name): try: if name[:3] == 'get': return _Getter(self.__dict__, name[3:]) elif name[:3] == 'set': return _Setter(self, name[3:]) except KeyError: pass raise AttributeError Using lambda instead of __call__ gives a small speed increase but, ask you point out, changes the symantics. Neil
- Previous message (by thread): Getters and Setters
- Next message (by thread): Getters and Setters
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list