Created
November 22, 2019 12:16
-
-
Save me2beats/60e9e60e6bae016b032d7de759f919d5 to your computer and use it in GitHub Desktop.
system prototype
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 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