#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
>>> b = Bar()
>>> b.f()
TypeError: unbound method f() must be called with Foo instance as first argument (got nothing instead)
>>> Bar.f
<unbound method Foo.f>
>>> b.f
<unbound method Foo.f>
how can I set Bar.f as <unbound method Bar.f>
See if that works. I think it should.
But the real question is why you want to do this. Why can't you just
inherit from Foo?
STeVe
class Meta(type):
def __init__(cls, name, bases, dic):
def attributeError(*_):
raise AttributeError
for base in bases:
for dont in base.privates:
setattr(cls,dont,attributeError) #override private methods
class Foo(object):
privates=('f',)
def f(self):
pass
def g(self):
pass
class Bar(Foo):
__metaclass__ = Meta
b=Bar()
b.g()
b.f()
and it implies writing only one metaclass.
___________________________________
Yahoo! Mail: gratis 1GB per i messaggi e allegati da 10MB
http://mail.yahoo.it