Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy a member function of another class into myclass in python?

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?

like image 495
balki Avatar asked Mar 10 '12 11:03

balki


1 Answers

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?

like image 192
Tomasz Wysocki Avatar answered Nov 15 '22 11:11

Tomasz Wysocki