fork(2) download
  1. props={}
  2. def myProp(fn):
  3. props[fn.__name__]=fn
  4.  
  5. class test():
  6. def __init__(self):
  7. self.x=5
  8. def __getattribute__(self, name):
  9. if name in props.keys():
  10. return props[name](self)
  11. return object.__getattribute__(self, name)
  12. def __delattribute__(self, name):
  13. if name in props.keys():
  14. del props[name]
  15. return
  16. object.__delattribute__(self, name)
  17. def __setattr__(self, name, value):
  18. if name in props.keys():
  19. return props[name](self, value)
  20. return object.__setattr__(self, name, value)
  21.  
  22. @myProp
  23. def myfunc(self, value=None):
  24. if value:
  25. self.x=value*2
  26. return self.x-2
  27.  
  28. x= test()
  29. print(x.myfunc)
  30. x.myfunc=6
  31. print(x.myfunc)
Success #stdin #stdout 0.15s 10264KB
stdin
Standard input is empty
stdout
3
10