Python importing

前端 未结 4 528
春和景丽
春和景丽 2021-01-20 03:23

I have a file, myfile.py, which imports Class1 from file.py and file.py contains imports to different classes in fi

4条回答
  •  日久生厌
    2021-01-20 04:09

    Python doesn't automatically introduce anything into the namespace of myfile.py, but you can access everything that is in the namespaces of all the other modules.

    That is to say, if in file1.py you did from file2 import SomeClass and in myfile.py you did import file1, then you can access it within myfile as file1.SomeClass. If in file1.py you did import file2 and in myfile.py you did import file1, then you can access the class from within myfile as file1.file2.SomeClass. (These aren't generally the best ways to do it, especially not the second example.)

    This is easily tested.

提交回复
热议问题