Forgotten attachment (sorry): python hijack.
class A(object):
"""Class with two attribute sets: one set in the class itself, one in a proxied dictionary (called props here).
The classe has 'a' attribute for itself and (b,c) attributes in the proxee. They are accessed in the same way.
Adding/deleting attributes is not supported, which is OK. Redefining __getattr__ and __setattr__ implies the
necessity of accessing all attributes using __dict__."""
def __init__(self):
self.__dict__['props']={'b':2,'c':3}
self.__dict__['a']=1
def __getattr__(self,attr):
"Try to get attribute from the class itself, the try to fetch it from the proxied dictionary, then fail"
if attr in self.__dict__: return self.__dict__[attr]
elif attr in self.__dict__['props'].keys(): return self.__dict__['props'][attr]
else: raise AttributeError("Attribute "+attr+" doesn't exist")
def __setattr__(self,attr,value):
if attr in self.__dict__.keys(): self.__dict__[attr]=value
elif attr in self.__dict__['props'].keys(): self.__dict__['props'][attr]=value
else: raise AttributeError("Attribute "+attr+" doesn't exist")
a=A()
# test attribute access
print a.a,a.b,a.c
# non-existent attributes should throw
try:
a.d=4
print a.d
except AttributeError: pass
# assign to attributes
a.a=4; a.b=a.a+1; a.c=6
print a.a,a.b,a.c
_______________________________________________
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig