I have a class foo that inherits from bar. However I also want to have the option when initializing foo to have it inherit from wall instead of bar. I am thinking something like this:
class Foo():
def __init__(self, pclass):
self.inherit(pclass)
super().__init__()
Foo(Bar) # child of Bar
Foo(Wall) # child of Wall
Is this possible in Python?
Foo(Bar)andFoo(Wall)give you instances of different types, and those types don't exist statically, you have to create them on the fly. Which means you have to do this in__new__instead of__init__. For example,class subcls(cls, pclass): pass, thenreturn super().__new__(subcls). But I don't know what that buys you over a more idiomatic design, except for extra cleverness that makes your code harder to understand.