Skip to content

Instantly share code, notes, and snippets.

@zqqf16
Created December 26, 2013 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zqqf16/8130117 to your computer and use it in GitHub Desktop.
Save zqqf16/8130117 to your computer and use it in GitHub Desktop.
A simple code to learn meta class.
#!/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