How to replace an instance in __init__() with a different object?

后端 未结 3 687
Happy的楠姐
Happy的楠姐 2020-12-03 01:30

I am calling a constructor in ClassA and want to have the resulting object be of a different class (ClassB) if a certain condition is met. I\'ve tried replacing the first a

3条回答
  •  青春惊慌失措
    2020-12-03 01:50

    Don't try to pervert the purpose of constructors: use a factory function. Calling a constructor for one class and being returned an instance of a different class is a sure way to cause confusion.

提交回复
热议问题