Python imports across modules and global variables

后端 未结 4 672
遇见更好的自我
遇见更好的自我 2020-12-19 10:00

I have a question which seems to be rather fundamental but I can\'t seem to find any help on this anywhere.

file_a.py >>

from xyz import XYZ
class A:
         


        
4条回答
  •  既然无缘
    2020-12-19 10:58

    Yes, your understanding is wrong. Each module is its own namespace, and only things you explicitly import within that file are available in that namespace.

    Contrary to other answers, it is not particularly Pythonic to refer to file_a.XYZ, although this will work. Instead, you should import XYZ and sys at the top of file_b.

提交回复
热议问题