2

I was wondering if there is generic inheritance in python.

For example,

Class A(object):
  def foo():

Class B(object):
  def foo():


Class C(<someParentClass>):
  def bar():

so effectively, I would like to do something like

  myClass1 = C()<A>
  myClass2 = C()<B>

Im guessing this is not possible in python, but is there any other way to have a similar effect?

2
  • 4
    Python doesn't even have generics. Generics only make sense when there are static types and you don't want to write the same code for several types. Python code always accepts any objects (although it can and will error later if the given objects aren't usable). What are you really trying to do? Commented Jun 11, 2011 at 12:35
  • 1
    Sounds like you should read about "mix-in" classes: en.wikipedia.org/wiki/Mixin Commented Jun 11, 2011 at 12:53

2 Answers 2

10

There's nothing preventing it. Everything in Python is essentially generic. Everything is runtime, including class statements, so you can do something like:

def make_C(parent):
    class C(parent):
        def bar(self):
            ...
    return C

myClass1 = make_C(A)
myClass2 = make_C(B)

If you want the C class to be a little more descriptive in name or documentation, you can assign the __name__ and __doc__ attributes, or use the three-argument form of type() instead of the class statement to create it.

Sign up to request clarification or add additional context in comments.

2 Comments

Note, though, while nothing prevents a person from doing something like this, it isn't normal practice for Python. There are better, clearer, more Pythonic solutions to this issue. Generics are really a solution to a problem that doesn't exist in Python. Consider that in Python you can test your objects for the "foo" function easily and call it with the expected arguments regardless of parent classes.
I agree that generics are not a meaningful thing in Python, but creating a class in a function like this does have its uses. It's just a solution to a different kind of problem :)
2

You could derive C from object and use

class MyClass1(A, C):
    pass
class MyClass2(B, C):
    pass

There are many ways achieving the exact effect you described, but I think defining C as a mix-in class is the most idiomatic approach.

12 Comments

If the intent is for C to specialize A and B, you should inherit from C first (or it won't be able to override methods defined in A or B.
@Thomas: I interpreted the OP's intent in the way that C extends A and B. In his example, it does not overwrite any methods.
Yes, so did I. And I'm saying "you should inherit from C, or you will get different behaviour". If C extends A and B, it should come before A and B in the hierarchy.
@Thomas: Depends on the semantics you want. I tend to put the actual base class first and the mix-ins later. I usually don't want mix-ins to overwrite methods, and I definitely want the derived class to use the metaclass of the actual base class, not the one of some mix-in.
But if C is a mixin, it's not a subclass. It's different behaviour than the OP seems to want. It's not extending A or B. As for the metaclass, that's not how it works. The metaclass is determined by all the baseclasses; the most specialized metaclass is used (with a specialcase for classic classes, which always lose to a new-style class.) If the baseclasses have conflicting metaclasses (not counting classic classes), it'll be an error.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.