Created
December 26, 2013 05:29
-
-
Save zqqf16/8130117 to your computer and use it in GitHub Desktop.
A simple code to learn meta class.
This file contains 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
#!/usr/bin/env python | |
class Meta(type): | |
def __new__(self, name, bases, attrs): | |
print('Meta: __new__: {} | {} | {} | {}'.format(self, name, bases, attrs)) | |
return super(Meta, self).__new__(self, name, bases, attrs) | |
def __init__(cls, name, bases, attrs): | |
print('Meta: __init__: {} | {} | {} | {}'.format(cls, name, bases, attrs)) | |
super(Meta, cls).__init__(name, bases, attrs) | |
def __call__(cls, *args, **kwargs): | |
print('Meta: __call__: {}'.format(cls)) | |
return super(Meta, cls).__call__(*args, **kwargs) | |
class Tmp(object): | |
__metaclass__ = Meta | |
def __new__(cls, *args, **kwargs): | |
print('Tmp: __new__: {} | {} | {}'.format(cls, args, kwargs)) | |
return super(Tmp, cls).__new__(cls, args, **kwargs) | |
def __init__(self, *args, **kwargs): | |
print('Tmp: __init__: {} | {} | {}'.format(self, args, kwargs)) | |
super(Tmp, self).__init__(*args, **kwargs) | |
def __call__(self, *args, **kwargs): | |
print('Tmp: __call__: {} | {} | {}'.format(self, args, kwargs)) | |
super(Tmp, self).__call__(*args, **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment