accessing variables from main method in python idle
Question: So I know if I create a python file with no main method declared and then I
run it, I'm able to access the variables within that file from the idle, but
if I do declare a main method, then I cannot access any variables from the
idle after the main method has finished running.

Does anyone know if there's a workaround where I'm able to use methods in my
python program, while also being able to access variables from within them in
the idle?


Answer: If you declare variable inside a method/function they are only in scope for
the life of that method or function. You can't access them from outside. If
you want some variable to be available to you declare it in the global space
and then import like you would any 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 you acces to `some_var` however you will not be able to access
`another_var` unless you return form your function and save like this

    
    
    from file1 import bar
    
    another_var = bar()
    

You can access variable in a function while the function is running by using
the `pdb` library like so:

    
    
    >>> def foo(x):
        import pdb; pdb.set_trace()  # this is one of the rare times it's okay to import inside a function
        return x* 2
    
    >>> foo(5)
    > <pyshell#13>(3)foo()
    (Pdb) x
    5
    (Pdb)
    

`pdb` is a very useful debugging tool. It will help you see what id happening
inside of your functions should you start getting some weird output. You can
read more about it [here](https://docs.python.org/2/library/pdb.html)

