scope - Python: Imported functions access to global variables -
i have 2 files shown below.
#foobar.py foo = false def bar(): if foo: print "worked" else: print "did not work"
and
#main.py foobar import * foo = true bar() print foo
the output when run $ python main.py
is
did not work true
is possible bar()
access global foo
defined in main.py
?
update
should have clarified earlier thought question should remain bare-bone possible.
foobar.py
supporting file (with other functions having different parameters use global foo
) want use bunch of related ipython notebooks.
the notebooks should ideally have own definitions of foo
, it's absence should caught , handled.
i chose not include argument try , make code cleaner.
i aware that:
wildcard imports ( import * ) should avoided, make unclear names present in namespace, confusing both readers , many automated tools.-pep8
accepting comment l3viathan answer.
no, it's not. why don't want use arguments? – l3viathan apr 22 @ 19:15
python: imported functions access global variables
Comments
Post a Comment