Last active
September 12, 2015 18:25
-
-
Save apua/a59aeaeca2f2bfa71049 to your computer and use it in GitHub Desktop.
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
######## | |
# auto # | |
######## | |
class M(type): | |
def __new__(mcls, *a, **kw): | |
print("in metaclass M - __new__:", locals()) | |
return super().__new__(mcls, *a, **kw) | |
def __init__(cls, name, base, dicts): | |
print("in metaclass M - __init__:", locals()) | |
def __call__(cls, *a, **kw): | |
print("in metaclass M - __call__:", locals()) | |
return super(M, cls).__call__(*a, **kw) | |
class C(metaclass=M): | |
print('===== C code start =====') | |
def __new__(cls, *a, **kw): | |
print("in class C - __new__:", locals()) | |
return super(C, cls).__new__(cls) | |
def __init__(self, *a, **kw): | |
print("in class C - __init__:", locals()) | |
print("~~~~~ C code finish ~~~~~~~") | |
C(1,2,3) | |
""" | |
===== C code start ===== | |
~~~~~ C code finish ~~~~~~~ | |
in metaclass M - __new__: {'mcls': <class '__main__.M'>, 'kw': {}, 'a': ('C', (), {'__qualname__': 'C', '__module__': '__main__', '__init__': <function C.__init__ at 0xffee7078>, '__new__': <function C.__new__ at 0xffecadf8>}), '__class__': <class '__main__.M'>} | |
in metaclass M - __init__: {'name': 'C', 'dicts': {'__qualname__': 'C', '__module__': '__main__', '__init__': <function C.__init__ at 0xffee7078>, '__new__': <function C.__new__ at 0xffecadf8>}, 'base': (), 'cls': <class '__main__.C'>} | |
in metaclass M - __call__: {'kw': {}, '__class__': <class '__main__.M'>, 'a': (1, 2, 3), 'cls': <class '__main__.C'>} | |
in class C - __new__: {'kw': {}, '__class__': <class '__main__.C'>, 'a': (1, 2, 3), 'cls': <class '__main__.C'>} | |
in class C - __init__: {'kw': {}, 'a': (1, 2, 3), 'self': <__main__.C object at 0xffeda8d0>} | |
""" | |
########## | |
# manual # | |
########## | |
class M(type): | |
def __new__(mcls, *a, **kw): | |
print("in metaclass M - __new__:", locals()) | |
return super().__new__(mcls, *a, **kw) | |
def __init__(cls, name, base, dicts): | |
print("in metaclass M - __init__:", locals()) | |
def __call__(cls, *a, **kw): | |
print("in metaclass M - __call__:", locals()) | |
return super(M, cls).__call__(*a, **kw) | |
print('===== C code start =====') | |
class C: | |
def __new__(cls, *a, **kw): | |
print("in class C - __new__:", locals()) | |
return super(C, cls).__new__(cls) | |
def __init__(self, *a, **kw): | |
print("in class C - __init__:", locals()) | |
print("~~~~~ C code finish ~~~~~~~") | |
C = M(C.__name__, C.__bases__, {'__module__': C.__module__, '__new__': C.__new__, | |
'__init__': C.__init__, '__qualname__': C.__qualname__}) | |
# which is same as: | |
# >>> name, base, dict_ = (C.__name__, C.__bases__, | |
# >>> {'__module__': C.__module__, '__init__': C.__init__, | |
# >>> '__new__': C.__new__, '__qualname__': C.__qualname__}) | |
# >>> C = M.__new__(M, name, base, dict_) | |
# >>> M.__init__(C, name, base, dict_) | |
# C(1,2,3) | |
C.__call__(1,2,3) | |
# which will do at the end: | |
# >>> c = C.__new__(C, 1, 2, 3) | |
# >>> C.__init__(c, 1, 2, 3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment