accessing variables from main method in python idle -


so know if create python file no main method declared , run it, i'm able access variables within file idle, if declare main method, cannot access variables idle after main method has finished running.

does know if there's workaround i'm able use methods in python program, while being able access variables within them in idle?

if declare variable inside method/function in scope life of method or function. can't access them outside. if want variable available declare in global space , import other function/class.

file1.py

some_var = whatever  def foo():     another_var = 42  def bar():     return 42 

file2.py

from file1 import some_var 

will give acces some_var not able access another_var unless return form function , save this

from file1 import bar  another_var = bar() 

you can access variable in function while function running using pdb library so:

>>> def foo(x):     import pdb; pdb.set_trace()  # 1 of rare times it's okay import inside function     return x* 2  >>> foo(5) > <pyshell#13>(3)foo() (pdb) x 5 (pdb) 

pdb useful debugging tool. see id happening inside of functions should start getting weird output. can read more here


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -