Created
November 10, 2020 01:34
-
-
Save warabanshi/85def811833fcf21d6b21d194c6a88cf to your computer and use it in GitHub Desktop.
see parameters of a descriptor
This file contains 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 Attribute(object): | |
def __init__(self, param=None, name='default'): | |
self.param = param | |
self.name = name | |
def __get__(self, instance, objtype): | |
return getattr(instance, self.get_private_name()) | |
def __set__(self, instance, val): | |
setattr(instance, self.get_private_name(), val) | |
def get_private_name(self): | |
return '_' + self.name | |
class Sample(object): | |
attr1 = Attribute(name='attr1', param=100) | |
attr2 = Attribute(name='attr2', param=200) | |
def __init__(self): | |
print('Sample::__init__() was called') | |
if __name__ == '__main__': | |
s = Sample() | |
print(vars(vars(Sample)['attr1'])) | |
print(vars(vars(Sample)['attr2'])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment