Last active
August 21, 2016 11:02
-
-
Save palankai/0e33200a381d311b198162ebb50b2263 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
class Metaclass(type): | |
def __new__(metacls, name, bases, namespace, **kwargs): | |
cls = type.__new__(metacls, name, bases, namespace) | |
print('>>> Metaclass.__new__(metacls={}, name={}, bases={}, namespace={}, **kwargs={})'.format( | |
metacls, name, bases, namespace, kwargs, cls | |
)) | |
print(cls) | |
return cls | |
def __init__(cls, name, bases, namespace, **kwargs): | |
print('>>> Metaclass.__init__(cls={}, name={}, bases={}, namespace={}, **kwargs={})'.format( | |
cls, name, bases, namespace, kwargs | |
)) | |
super().__init__(name, bases, namespace) | |
def __call__(cls, *args, **kwargs): | |
instance = super().__call__(*args, **kwargs) | |
print('>>> Metaclass.__call__(cls={}, *args={}, **kwargs={}'.format( | |
cls, args, kwargs | |
)) | |
print(instance) | |
return instance | |
@classmethod | |
def __prepare__(metacls, name, bases, **kwargs): | |
print('>>> Metaclall.__prepare__(metacls={}, name={}, bases={}, **kwargs={})'.format( | |
metacls, name, bases, kwargs | |
)) | |
print('{}') | |
return dict() | |
class Object(metaclass=Metaclass, metakw='metakw'): | |
def __new__(cls, *args, **kwargs): | |
res = super().__new__(cls) | |
print('>>> Object.__new__(cls=cls, *args={}, **kwargs={})'.format( | |
cls, args, kwargs | |
)) | |
print(res) | |
return res | |
def __init__(self, *args, **kwargs): | |
print('>>> Object.__init__(self={}, *args={}, **kwargs={})'.format( | |
self, args, kwargs | |
)) | |
>>> Metaclall.__prepare__(metacls=<class '__main__.Metaclass'>, name=Object, bases=(), **kwargs={'metakw': 'metakw'}) | |
{} | |
>>> Metaclass.__new__(metacls=<class '__main__.Metaclass'>, name=Object, bases=(), namespace={'__new__': <function Object.__new__ at 0x7fd0c3159400>, '__qualname__': 'Object', '__init__': <function Object.__init__ at 0x7fd0c3159488>, '__module__': '__main__'}, **kwargs={'metakw': 'metakw'}) | |
<class '__main__.Object'> | |
>>> Metaclass.__init__(cls=<class '__main__.Object'>, name=Object, bases=(), namespace={'__new__': <function Object.__new__ at 0x7fd0c3159400>, '__qualname__': 'Object', '__init__': <function Object.__init__ at 0x7fd0c3159488>, '__module__': '__main__'}, **kwargs={'metakw': 'metakw'}) | |
class Descendant(Object, newmetakw='newmetakw'): | |
def __new__(cls, *args, **kwargs): | |
res = super().__new__(cls) | |
print('>>> Descendant.__new__(cls=cls, *args={}, **kwargs={})'.format( | |
cls, args, kwargs | |
)) | |
print(res) | |
return res | |
def __init__(self, *args, **kwargs): | |
print('>>> Descendant.__init__(self={}, *args={}, **kwargs={})'.format( | |
self, args, kwargs | |
)) | |
super().__init__(*args, **kwargs) | |
>>> Metaclall.__prepare__(metacls=<class '__main__.Metaclass'>, name=Descendant, bases=(<class '__main__.Object'>,), **kwargs={'newmetakw': 'newmetakw'}) | |
{} | |
>>> Metaclass.__new__(metacls=<class '__main__.Metaclass'>, name=Descendant, bases=(<class '__main__.Object'>,), namespace={'__new__': <function Descendant.__new__ at 0x7fd0c3159510>, '__qualname__': 'Descendant', '__init__': <function Descendant.__init__ at 0x7fd0c3159598>, '__module__': '__main__'}, **kwargs={'newmetakw': 'newmetakw'}) | |
<class '__main__.Descendant'> | |
>>> Metaclass.__init__(cls=<class '__main__.Descendant'>, name=Descendant, bases=(<class '__main__.Object'>,), namespace={'__new__': <function Descendant.__new__ at 0x7fd0c3159510>, '__qualname__': 'Descendant', '__init__': <function Descendant.__init__ at 0x7fd0c3159598>, '__module__': '__main__'}, **kwargs={'newmetakw': 'newmetakw'}) | |
# Run time | |
>>> Object("arg", kwarg="kwargs") | |
>>> Object.__new__(cls=cls, *args=<class '__main__.Object'>, **kwargs=('arg',)) | |
<__main__.Object object at 0x7fd0c31637f0> | |
>>> Object.__init__(self=<__main__.Object object at 0x7fd0c31637f0>, *args=('arg',), **kwargs={'kwarg': 'kwarg'}) | |
>>> Metaclass.__call__(cls=<class '__main__.Object'>, *args=('arg',), **kwargs={'kwarg': 'kwarg'}) | |
<__main__.Object object at 0x7fd0c31637f0> | |
<__main__.Object object at 0x7fd0c31637f0> | |
>>> Descendant("iarg", ikwarg="ikwargs") | |
>>> Object.__new__(cls=cls, *args=<class '__main__.Descendant'>, **kwargs=()) | |
<__main__.Descendant object at 0x7fd0c31638d0> | |
>>> Descendant.__new__(cls=cls, *args=<class '__main__.Descendant'>, **kwargs=('iarg',)) | |
<__main__.Descendant object at 0x7fd0c31638d0> | |
>>> Descendant.__init__(self=<__main__.Descendant object at 0x7fd0c31638d0>, *args=('iarg',), **kwargs={'ikwarg': 'ikwarg'}) | |
>>> Object.__init__(self=<__main__.Descendant object at 0x7fd0c31638d0>, *args=('iarg',), **kwargs={'ikwarg': 'ikwarg'}) | |
>>> Metaclass.__call__(cls=<class '__main__.Descendant'>, *args=('iarg',), **kwargs={'ikwarg': 'ikwarg'}) | |
<__main__.Descendant object at 0x7fd0c31638d0> | |
<__main__.Descendant object at 0x7fd0c31638d0> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment