0

I have two the python classes. I wonder if I can do something like

file A

# file A.py
from B import B
class A(){
    def Foo():
        pass

}

file B

# file B.py
from A import A
class B(){
    def Bar():
        pass

}

2 Answers2

1

Yes you can, however, you might be better advised to use different names for the files and the classes they contain.

Please note that the use of {} is not a python construct to mark scope boundaries

file a.py

# file a.py
from b import B
class A()
    def Foo():
        pass

file b.py

# file b.py
from a import A
class B()
    def Bar():
        pass

This type of circular import will work, but there are better ways, notably placing imports in an __init__.py file

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
0

No, you can't. Cross reference can be achieve by local import. See below example

File A:

# file A.py
from B import B
class A():
    def Foo():
        pass

File B:

# file B.py
class B():
    def Bar():
        pass

def some_function():
    from A import A
    # use A here
Woody Huang
  • 462
  • 5
  • 11