Skip to content

Instantly share code, notes, and snippets.

@fernandojunior
Last active December 30, 2015 10:13
Show Gist options
  • Save fernandojunior/a468b5d7726e978a3e80 to your computer and use it in GitHub Desktop.
Save fernandojunior/a468b5d7726e978a3e80 to your computer and use it in GitHub Desktop.
Class name using metaclass in python
class Manager(object):
def __init__(self, cls_):
self.cls_ = cls_
def all(self):
print(self.cls_.__name__)
class ModelMeta(type):
def __init__(cls, name, bases, attrs):
super().__init__(name, bases, attrs)
setattr(cls, 'objects', Manager(cls))
class Model(metaclass=ModelMeta):
pass
class Node(Model):
pass
Node.objects.all()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment