(Jul-16-2021, 05:56 AM)Skaperen Wrote: i have a class that, during __init__(), detects a situation where a different class, not itself, needs to be provided to the caller. but Python classes don't work by returning a reference to the class. is there a way to substitute an instance of a different class, instead?I think you are a more experienced programmer than I am but you seem to not want to just try things.
We don't need to see your full project files just some basic classes representing what you are trying to do.
I came up with the following based on your description it may be nothing like what you want but perhaps you can adjust it to give an indication of what you are after.
from dataclasses import dataclass @dataclass class AClass: situation: bool def __new__(cls, situation: bool): if situation: return DifferentClass(situation) return super().__new__(cls) @dataclass class DifferentClass: situation: bool a_class = AClass(situation=False) different_class = AClass(situation=True) print(a_class) print(different_class)
Output:AClass(situation=False)
DifferentClass(situation=True)