Skip to content

Instantly share code, notes, and snippets.

@zaltoprofen
Created February 26, 2014 17:22
Show Gist options
  • Save zaltoprofen/9234147 to your computer and use it in GitHub Desktop.
Save zaltoprofen/9234147 to your computer and use it in GitHub Desktop.
class MyMeta(type):
def __new__(meta_cls, name, bases, dic):
print '__new__(meta_cls=%s, name=%s, bases=%s, dic=%s)' % (meta_cls, name, bases, dic)
return type.__new__(meta_cls, name, bases, dic)
def __init__(cls, name, bases, dic):
from inspect import isfunction
print '__init__(cls=%s, name=%s, bases=%s, dic=%s)' % (cls, name, bases, dic)
type.__init__(cls, name, bases, dic)
for k, v in cls.__dict__.iteritems():
if isfunction(v):
setattr(cls, k, MyMeta.sugarcoat(name, k, v))
setattr(cls, 'common_method', MyMeta.common_method)
@staticmethod
def sugarcoat(class_name, method_name, method):
def _method(self, *args, **kwds):
print 'call : %s#%s()' % (class_name, method_name)
method(self, *args, **kwds)
print 'return : %s#%s()' % (class_name, method_name)
return _method
@staticmethod
def common_method(self):
print 'howdy!'
class Klass1:
__metaclass__ = MyMeta
def hoge(self):
print "fuga"
class Klass2:
__metaclass__ = MyMeta
def foo(self):
print 'foo'
class SubKlass1(Klass1):
def new_method(self):
print 'hello!'
class SubKlass2(Klass2):
def foo(self):
super(SubKlass2, self).foo()
print 'hallo!'
# unreachable
def common_method(self):
print 'guten Tag!'
print '='*10, 'Klass1', '='*10
Klass1().hoge()
Klass1().common_method()
print '='*10, 'Klass2', '='*10
Klass2().foo()
Klass2().common_method()
print '='*10, 'SubKlass1', '='*10
SubKlass1().hoge()
SubKlass1().common_method()
SubKlass1().new_method()
print '='*10, 'SubKlass2', '='*10
SubKlass2().foo()
SubKlass2().common_method()
# output
#
#__new__(meta_cls=<class '__main__.MyMeta'>, name=Klass1, bases=(), dic={'__module__': '__main__', '__metaclass__': <class '__main__.MyMeta'>, 'hoge': <function hoge at 0x10caee848>})
#__init__(cls=<class '__main__.Klass1'>, name=Klass1, bases=(), dic={'__module__': '__main__', '__metaclass__': <class '__main__.MyMeta'>, 'hoge': <function hoge at 0x10caee848>})
#__new__(meta_cls=<class '__main__.MyMeta'>, name=Klass2, bases=(), dic={'__module__': '__main__', 'foo': <function foo at 0x10cb59d70>, '__metaclass__': <class '__main__.MyMeta'>})
#__init__(cls=<class '__main__.Klass2'>, name=Klass2, bases=(), dic={'__module__': '__main__', 'foo': <function foo at 0x10cb59d70>, '__metaclass__': <class '__main__.MyMeta'>})
#__new__(meta_cls=<class '__main__.MyMeta'>, name=SubKlass1, bases=(<class '__main__.Klass1'>,), dic={'__module__': '__main__', 'new_method': <function new_method at 0x10cb59e60>})
#__init__(cls=<class '__main__.SubKlass1'>, name=SubKlass1, bases=(<class '__main__.Klass1'>,), dic={'__module__': '__main__', 'new_method': <function new_method at 0x10cb59e60>})
#__new__(meta_cls=<class '__main__.MyMeta'>, name=SubKlass2, bases=(<class '__main__.Klass2'>,), dic={'common_method': <function common_method at 0x10cb5b050>, '__module__': '__main__', 'foo': <function foo at 0x10cb59f50>})
#__init__(cls=<class '__main__.SubKlass2'>, name=SubKlass2, bases=(<class '__main__.Klass2'>,), dic={'common_method': <function common_method at 0x10cb5b050>, '__module__': '__main__', 'foo': <function foo at 0x10cb59f50>})
#========== Klass1 ==========
#call : Klass1#hoge()
#fuga
#return : Klass1#hoge()
#howdy!
#========== Klass2 ==========
#call : Klass2#foo()
#foo
#return : Klass2#foo()
#howdy!
#========== SubKlass1 ==========
#call : Klass1#hoge()
#fuga
#return : Klass1#hoge()
#howdy!
#call : SubKlass1#new_method()
#hello!
#return : SubKlass1#new_method()
#========== SubKlass2 ==========
#call : SubKlass2#foo()
#call : Klass2#foo()
#foo
#return : Klass2#foo()
#hallo!
#return : SubKlass2#foo()
#howdy!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment