Last active
December 23, 2020 11:07
-
-
Save gevorgyana/35a5d698b93bd6e20ca4d94892e755e0 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 Desc: | |
def __set_name__(self, owner, name): | |
self.field_name = name | |
def __get__(self, instance, *args): | |
if self.field_name in instance.__dict__.keys(): | |
return instance.__dict__[self.field_name] | |
else: | |
return "NoneDescriptor" | |
def __set__(self, instance, value, *args): | |
instance.__dict__[self.field_name] = value | |
class Meta(type): | |
# HERE HERE HERE | |
cnt: int = 0 | |
def __new__(cls, *args): | |
# HERE HERE HERE | |
args[2]['_{}__id'.format(cls.__name__)] = Meta.cnt | |
rv = super().__new__(cls, *args) | |
assert( | |
'_Meta__id' in rv.__dict__.keys() | |
) | |
Meta.cnt = Meta.cnt + 1 | |
return rv | |
def __init__(self, *args): | |
super().__init__(*args) | |
def __call__(self, *args): | |
return super().__call__(*args) | |
class Type(metaclass=Meta): | |
foo = Desc() | |
bar = Desc() | |
b = Type() | |
print(b.foo) | |
b.foo = "ff" | |
print(b.foo) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment