Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- >>> class descr:
- ... def __get__(self, obj, cls):
- ... if obj is None:
- ... print("Retrieved from class")
- ... else:
- ... print("Retrieved from instance")
- ... def __set__(self, obj, val):
- ... print("Set on instance")
- ... def __delete__(self, obj):
- ... print("Deleted from instance")
- ...
- >>> class Demo:
- ... attr = descr()
- ...
- >>> Demo().attr
- Retrieved from instance
- >>> Demo().attr = 1
- Set on instance
- >>> del Demo().attr
- Deleted from instance
- >>> Demo.attr
- Retrieved from class
- >>> Demo.attr = 1
- >>> Demo.attr
- 1
- >>> Demo.attr = descr()
- >>> Demo.attr
- Retrieved from class
- >>> del Demo.attr
- >>> Demo.attr
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- AttributeError: type object 'Demo' has no attribute 'attr'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement