B is...">

Why are such imports not allowed?

FILE: b.py

class B:
    def __init__(self):
        print "B"

import a

a = A()

FILE: a.py

class A(B):             ###=> B  is not  defined
    def __init__(self):
        print "A"

When I try to execute b.py, he said that B is not defined. I do not understand the "import"?

Thank you very much if you can point out the problem.

+3
source share
2 answers

The next work on your code will be:

==== FILE: b.py ====

class B:
    def __init__(self):
        print "B"

import a

if __name__ == "__main__":
    a = a.A()

==== FILE: a.py ====
import b

class A(b.B):             ###=> B  is not  defined
    def __init__(self):
        print "A"

Please note the differences:

  • Files (modules) are namespaces, if you import "a", you refer to its class A as "aA".

  • You need to import b into a.py if you want to use it.

, , , . , , .

+5

python A . C PHP, .

B , A, . B c.py "from c import B".

+5

Source: https://habr.com/ru/post/1709014/


All Articles