Skip to content Skip to sidebar Skip to footer

Python Inheritance: Choose Parent Class Using Argument

I'm having trouble designing some classes. I want my user to be able to use the Character() class by passing in an argument for the type of character (e.g. fighter/wizard). Dummy

Solution 1:

You can make use of the less known feature of the type function:

defCharacter(char_class):
    returntype("Character", (char_class,), {})

type can be used to dynamically create a class. First parameter is the class name, second are the classes to inherit from and third are the initial attributes.

Post a Comment for "Python Inheritance: Choose Parent Class Using Argument"