Skip to content

Instantly share code, notes, and snippets.

@me2beats
Created November 22, 2019 12:16
Show Gist options
  • Save me2beats/60e9e60e6bae016b032d7de759f919d5 to your computer and use it in GitHub Desktop.
Save me2beats/60e9e60e6bae016b032d7de759f919d5 to your computer and use it in GitHub Desktop.
system prototype
class NonCallable:
#TODO: now this is a behavior, not metaclass,
# so msg has to refer to NonCallable
def __call__(self, *args, **kwargs):
is_root = len(self.__mro__) ==2
cls_name = self.__name__
type_name = type(self).__name__
if is_root:
msg = '\nClass '+cls_name+'(metaclass = '+type_name+') cannot be called'
else:
mcs_root_name = self.get_metaclass_root().__name__
msg = '\nClass '+cls_name+' cannot be called\nbecause it has '+mcs_root_name+'(metaclass = '+type_name+')'
raise AttributeError(msg)
def get_metaclass_root(self):
return (self.__mro__[::-1][1])
class ClassAllMethods:
def __new__(cls, name, bases, attrs):
for method_name, method in attrs.items():
if method_name.startswith('_'): continue
print (method)
attrs[method_name] = classmethod(method)
return type.__new__(cls, name, bases, attrs)
class SystemType(NonCallable, ClassAllMethods, type):
pass
class System(metaclass=SystemType):
def some_method(cls):
print('hi')
class MySystem(System):
def some_method(cls):
super().some_method()
print('hi')
MySystem.some_method()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment