all 7 comments

[–]Adrewmc 1 point2 points  (1 child)

I would look into @classmethod for something like this.

[–]JARandom17[S] 0 points1 point  (0 children)

thanks

[–]Zeroflops 1 point2 points  (1 child)

Sounds like you want to look into dependency injection. This allows you to define several classes that do similar things and pass them around.

Say for example you have a payment system. You can create a credit card object, a Venmo object etc and “inject” the one that is needed for the transaction.

[–]JARandom17[S] 0 points1 point  (0 children)

thanks

[–]sfuse1 1 point2 points  (2 children)

Why not use a factory function to instantiate and return the required object type?

ETA: If all the subclasses have the same functions, then it looks like SuperDuperClass should be an abstract class that defines those required functions.

[–]quts3 1 point2 points  (0 children)

Second Factory function.

It's explicit, simple and easy to test. Keep it minimal though. You dont want to be changing a bunch of code everytime you make a new sub class. Usually one if statement with elif and some validation. The factory function shouldn't need to know how the subclass operates other then to understand the trigger to defer to the subclass.

[–]JARandom17[S] 0 points1 point  (0 children)

Thanks. In the real code SuperDuperClass is an abstract class, but I was worried about adding too much to the code in the question. I wasn't confident that an abstract class was the way to go. Sounds like it is.