Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class TypedField:
- def __init__(self, _type, _name):
- self._type = _type
- self._name = f'_{_name}'
- def __set__(self, instance, value):
- if not isinstance(value, self._type):
- raise TypeError("Only %s type is supported, you provided %s" % (self._type, type(value)))
- setattr(instance, self._name, value)
- def __get__(self, instance, owner):
- return getattr(instance, self._name)
- class PythonUZ:
- member_count = TypedField(int, 'member_count')
- title = TypedField(str, 'title')
- pyuz = PythonUZ()
- pyuz.member_count = 1914
- print(pyuz.member_count)
- pyuz.title = 42 # raises TypeError as we provided int instead of str
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement