How to do this?

F

flyaflya

the codes as follow:
class Base:
def fun(self):
print "base_fun"
def fun2(self):
print "base_fun2"

class Foo:
def __init__(self, base):
self.__base = base
def fun3(self):
print "foo_fun3"

b = Base()
foo = Foo(b)

my aim is:when "foo.fun3()" be called,it run as usually, but when
"foo.fun()" or "foo.fun2() be called, as Foo hasn't attribute "fun()" and
"fun2()", foo will call "self.__base.fun()" or "self.__base.fun2()".
when a attr can't be find in foo, it's will try to search 'self.__base". I
think this maybe need to use "__getattr__" and "__setattr__" ,but I cant do
it successfully. can someone help me?
 
D

Dan Sommers

the codes as follow:
class Base:
def fun(self):
print "base_fun"
def fun2(self):
print "base_fun2"
class Foo:
def __init__(self, base):
self.__base = base
def fun3(self):
print "foo_fun3"
b = Base()
foo = Foo(b)
my aim is:when "foo.fun3()" be called,it run as usually, but when
"foo.fun()" or "foo.fun2() be called, as Foo hasn't attribute "fun()" and
"fun2()", foo will call "self.__base.fun()" or "self.__base.fun2()".
when a attr can't be find in foo, it's will try to search 'self.__base". I
think this maybe need to use "__getattr__" and "__setattr__" ,but I cant do
it successfully. can someone help me?

class Foo(Base):
def fun3(self):
print "fun_foo3"

HTH,
Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,311
Members
47,981
Latest member
satome

Latest Threads

Top