Skip to content

Instantly share code, notes, and snippets.

@gevorgyana
Created December 23, 2020 10:51
Show Gist options
  • Save gevorgyana/81765bd39a43991b98f9b49c18377956 to your computer and use it in GitHub Desktop.
Save gevorgyana/81765bd39a43991b98f9b49c18377956 to your computer and use it in GitHub Desktop.
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):
rv = super().__new__(cls, *args)
# HERE HERE HERE
setattr(rv, '_{}__id'.format(cls.__name__), Meta.cnt)
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