Created
December 24, 2020 10:01
-
-
Save gevorgyana/0ae81740f750127916e60a4ca17b4d93 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Descriptor: | |
def __get__(self, instance, *args): | |
return getattr(instance, self.attribute_name, 'None') | |
def __set__(self, instance, value): | |
setattr(instance, self.attribute_name, value) | |
def __delete__(self, instance): | |
delattr(instance, self.attribute_name) | |
def __set_name__(self, owner, name): | |
self.attribute_name = name + "_hidden_part" | |
class User: | |
field = Descriptor() | |
u = User() | |
print(u.field) | |
u.field = 1 | |
print(u.field) | |
del u.field |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment