Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. class IntField(object):
  2. def __set_name__(self, owner, name):
  3. # Python 3.6+ only. On previous versions, you'll need a metaclass or some other workaround to get the name.
  4. self.name = name
  5. def __get__(self, instance, owner):
  6. if instance is None:
  7. return self
  8. return instance.__dict__[self.name]
  9. def __set__(self, instance, value):
  10. if not isinstance(value, int):
  11. raise ValueError
  12. instance.__dict__[self.name] = IntFieldVal(value)
  13.  
  14. class IntFieldVal(int):
  15. def make_siren_noises_i_dunno(self):
  16. print('weeoo weeoo weeoo')
  17.  
  18. class Foo(object):
  19. field = IntField()
  20.  
  21. x = Foo()
  22. y = Foo()
  23. x.field = 3
  24. y.field = 4
  25. print(x.field == 3)
  26. print(y.field == 4)
  27. x.field.make_siren_noises_i_dunno()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement