Namespaces with Module Imports

后端 未结 5 1252
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:52

    As others have said, there isn't actually a global pi in your module1. A good solution for you is this, which only imports pi once from math and explicitly ensures that the pi you're getting is the one from module1:

    main.py:

    import module1
    
    def wow():
        print module1.pi
    
    wow()
    module1.cool()
    

    module1.py:

    from math import pi
    
    def cool():
        print pi
    

提交回复
热议问题