Namespaces with Module Imports

后端 未结 5 1260
Happy的楠姐
Happy的楠姐 2020-11-27 18:06

I am learning Python and am still a beginner, although I have been studying it for about a year now. I am trying to write a module of functions which is called within a main

5条回答
  •  清歌不尽
    2020-11-27 18:30

    "Explicit is better than implicit" is a design decision that was made by the creators of Python (launch python and run import this).

    Therefore, when you run module1.cool(), Python will not look for the undefined pi in the main module.


    You'll have to import the math module in explicitly whenever you want to use it - that's just how Python works.

    Also, you should avoid from X import *-style imports, that's bad practice too. Here, you could do: from math import pi.

提交回复
热议问题