Skip to content

Instantly share code, notes, and snippets.

@sigmavirus24
Created December 31, 2017 01:38
Show Gist options
  • Save sigmavirus24/755897382e92e2e5df502148769191dd to your computer and use it in GitHub Desktop.
Save sigmavirus24/755897382e92e2e5df502148769191dd to your computer and use it in GitHub Desktop.
# Python 3.3 only
class klass(object):
def __init__(self, name, klass, bases, children=None):
self.name = name
self.klass = klass
self.bases = bases
self.children = children or []
def __repr__(self):
return '{} {}({})'.format(self.klass, self.name,
[b.__name__ for b in self.bases])
class SubClassTracker(type):
tracker = {}
def __new__(cls, name, base_classes, attrs):
SubClassTracker.tracker[name] = klass(name, cls, base_classes)
for b in base_classes:
bname = b.__name__
if bname in SubClassTracker.tracker:
SubClassTracker.tracker[bname].children.append(name)
return type.__new__(cls, name, base_classes, attrs)
class ParentClass(metaclass=SubClassTracker):
def __init__(self, foo):
self.foo = foo
def __repr__(self):
return 'ParentClass(foo={})'.format(self.foo)
class ChildClass(ParentClass):
def __init__(self, foo, bar):
super(ChildClass, self).__init__(foo)
self.bar = bar
def __repr__(self):
return 'ChildClass(foo={}, bar={})'.format(self.foo, self.bar)
class ChildClass2(ParentClass):
def __init__(self):
super(ChildClass2, self).__init__(True)
def __repr__(self):
return 'ChildClass2()'
def InbredGrandchildClass(ChildClass, ChildClass2):
def __init__(self):
self.bar = 1
c = ChildClass(1, 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment