1

Suppose I have a class X and a class Y, is it possible to dynamically define a class that has X as a base, but also methods of Y.

def combiner(base_class, derived_class):
   return derived_class(base_class) # returns a class

some_class = combiner(x, Y)
my_object = some_class()

This is somewhat of a pseudocode, is it possible to make something like this work in python?

user623949
  • 101
  • 1
  • 7
  • Define "combine". Do you mean that you want to dynamically create a child class of a base class? – timgeb Jul 06 '20 at 13:58
  • 1
    Interesting question, but I think you need to be more specific. What is the expected result - a class with all the methods and fields of the base and derived classes? Should it be an instance of each class (as in (`isinstance(newClass(), X)==True`, same for `Y`? – kabanus Jul 06 '20 at 14:01
  • 1
    The `base(derived)` is also confusing. Usually the derived class builds its parent, this seems more like a mix-in. Are `X` and `Y` actually parent and child classes? I suggest you post your exact problem for more accurate answers. – kabanus Jul 06 '20 at 14:02
  • Please see my edits, tell me if still unclear. I want to combine the two classes such that the new class has one of them as base, but the inheritance is specified by the other class like in the example. – user623949 Jul 06 '20 at 14:17
  • 2
    Does this answer your question? [Dynamic inheritance in Python](https://stackoverflow.com/questions/21060073/dynamic-inheritance-in-python) – Joshua Nixon Jul 06 '20 at 14:18

0 Answers0