How to copy a member function of another class int

2019-04-06 10:50发布

I have a utility class from which I want to use one of the member function in another class. I don't want to inherit from that class. I just want to re-use the code from one of the member function of the other class. Kind of partial inheritance.

class HugeClass():
   def interestedFunc(self,arg1):
      doSomething(self.someMember1)
   def OtherFunctions(self):
      ...



class MyClass():
   def __init__(self):
      self.someMember1 = "myValue"
      self.interestedFunc = MagicFunc(HugeClass.interestedFunc)

c = MyClass()
print c.interestedFunc(arg)

Is there such a MagicFunc in python?

1条回答
Root(大扎)
2楼-- · 2019-04-06 11:20

You can do what you want ie.:

class Foo(object):
    def foo(self):
        print self.a

class Bar(object):
    foo = Foo.__dict__['foo']

b = Bar()
b.a = 1
b.foo()

But are you sure that this is good idea?

查看更多
登录 后发表回答