1

Consider the following situation:

#module.py
def test():
    foo = bar+'foo'
    print foo
if __name__ == '__main__':
    bar='test'
    test()

The main file is:

#main.py
import module
bar = 'test'
module.test()

Running main.py obviously generates a NameError. I know that there are several ways to fix this (such for example, redefining the function in main.py) but my question is: how is it possible to add the bar variable to the function scope, without modifying the module.py code and, more generally, what is it considered best-practice in such a situation?

Edit: What about the same question using from module import *?

Pierpaolo
  • 1,721
  • 4
  • 20
  • 34

0 Answers0