python - Make global function known for imported function -


i writing ipythonnotebook , make code less clustered, defining functions not in main notebook. outplace them in external .py-files import.

why doesn't test() know of u()?

in mymodule.py-file

def test():     number = u()+u()     return number 

and main file (in notebook)

from mymodul import test  def u():     bla = 1     return bla   test() 

my test()-function imported well, not know u:

nameerror: global name 'u' not defined

you cannot this; globals ever looked in module function defined in.

you'd instead give function parameter accepts u argument:

def test(u):     number = u()+u()     return number 

and in main file:

def u():     bla = 1     return bla  test(u) 

if python worked way expected to, you'd create many hard-to-trace problems, namespaces (like modules) meant solve in first place.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -