K
kyo guan
How to Adding Functionality to a Class by metaclass(not by inherit)
#example:
import inspect
class Foo(object):
def f(self):
pass
def g(self):
pass
class MetaFoo(type):
def __init__(cls, name, bases, dic):
super(MetaFoo, cls).__init__(name, bases, dic)
for n, f in inspect.getmembers(Foo, inspect.ismethod):
setattr(cls, n, f)
#Bar want to achieve Foo's part/all functionality, but not by inherit
class Bar(object):
__metaclass__ = MetaFoo
<unbound method Foo.f>
how can I set Bar.f as <unbound method Bar.f>
#example:
import inspect
class Foo(object):
def f(self):
pass
def g(self):
pass
class MetaFoo(type):
def __init__(cls, name, bases, dic):
super(MetaFoo, cls).__init__(name, bases, dic)
for n, f in inspect.getmembers(Foo, inspect.ismethod):
setattr(cls, n, f)
#Bar want to achieve Foo's part/all functionality, but not by inherit
class Bar(object):
__metaclass__ = MetaFoo
<unbound method Foo.f>
how can I set Bar.f as <unbound method Bar.f>