26

I'm creating a class (class0) in Python that is a currently based off of one class (class1); however, I'd like to inherit from another class as well (class2). The thing about class2 is that I don't want all of it's methods and attributes, I just need one single method. Is it possible for class0 to only inherit a single method form a class2?

rectangletangle
  • 50,393
  • 94
  • 205
  • 275
  • check out [How to inherit only a small part of methods from a large class?](https://www.sololearn.com/Discuss/1314620/how-to-inherit-only-a-small-part-of-methods-from-a-large-class-a-into-a-new-class-b-without). Best answer indicates: your class design might be messed up, if you want to do that. – PythoNic Aug 12 '22 at 12:32

4 Answers4

27

The "mixin" method of having another class that just implemetns the method you want is the correct thing to do in this case. But for sake of completeness, as it answers exactly what you are asking, I add that yes, it is possible to have a behavior just like the "partial inheritance" you want (but note that such a concept does not exist formally).

All one have to do is to add member on the new class that refer to to the method or attribute you wish to repeat there:

class Class2(object):
   def method(self):
      print ("I am method at %s" % self.__class__)

class Class1(object):
   pass

class Class0(Class1):
   method = Class2.__dict__["method"]

ob = Class0()
ob.method()

Note that retrieving the method from the class __dict__ is needed in Python 2.x (up to 2.7) - due to runtime transforms that are made to convert the function in a method. In Python 3.0 and above, just change the line

method = Class2.__dict__["method"]

to

method = Class2.method
jsbueno
  • 99,910
  • 10
  • 151
  • 209
15

One path is to use the 'mixin' approach:

class Mixin(object):
    def method(self):
        pass

class C1(Mixin, ...parents...):
    pass

class C2(Mixin, ...parents...):
    pass

Another way is the composition:

class C1(object):
    def method(self):
        pass

class C2(object):
    def __init__(self):
        self.c1 = C1()

    def method(self):
        return self.c1.method()
zindel
  • 1,837
  • 11
  • 13
  • In Python the first one wouldn't be called a Mixin but just a base class … – filmor Apr 13 '11 at 09:45
  • filmor, I don't think so. Mixin's method will be called first in this example and hide the `method` method in any other parents. – zindel Apr 13 '11 at 09:50
6

Why don't you use composition instead? Have your class keep a reference to the desired object, and delegate to it.

Geo
  • 93,257
  • 117
  • 344
  • 520
  • And having to pass any attributes he wishes this method to access by parameter to it. He might as well not use a method at all, just a function. – jsbueno Apr 13 '11 at 11:49
  • If that's the case, it is possible to just set up the attributes on the main object and call the unbound method from the other class. However, it sounds more like there should be a type-agnostic implementation of the algorithm as a top-level function that both classes bring into their local namespace as a method. – ncoghlan Apr 13 '11 at 12:38
4

create another base class implementing this one method and make class2 and class1 both inherit this class

Andrey Sboev
  • 7,454
  • 1
  • 20
  • 37