Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class IntField(object):
- def __set_name__(self, owner, name):
- # Python 3.6+ only. On previous versions, you'll need a metaclass or some other workaround to get the name.
- self.name = name
- def __get__(self, instance, owner):
- if instance is None:
- return self
- return instance.__dict__[self.name]
- def __set__(self, instance, value):
- if not isinstance(value, int):
- raise ValueError
- instance.__dict__[self.name] = IntFieldVal(value)
- class IntFieldVal(int):
- def make_siren_noises_i_dunno(self):
- print('weeoo weeoo weeoo')
- class Foo(object):
- field = IntField()
- x = Foo()
- y = Foo()
- x.field = 3
- y.field = 4
- print(x.field == 3)
- print(y.field == 4)
- x.field.make_siren_noises_i_dunno()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement