0

I want to implement a class in Python that returns an instance of another class when executed.

from Bmodule import B
class A:
    def __new__(cls):
        return B

And in the module Bmodule:

from Amodule import A
class B(A):
    def __init__(self):
        print("Class B was built")

But this is giving me an import error, probably due to the circular import. I know I could put both classes in a unique module, but I prefer to have them in separate ones. How can I fix it?

Thanks!!

1 Answers1

1

Something like this:

class A:
    def __new__(cls):
        from Bmodule import B
        return super().__new__(B)

class B(A):
    def __init__(self):
        print("Class B was built")

print(A())
Alex Hall
  • 34,833
  • 5
  • 57
  • 89