How can I automatically pass variables out of the function? How to pass variables from inner to the outer scope?

I do a project in Python and Julia and have codes within functions such as (Julia version):

function foo(a,b)
    c = a+b
    #more code here
end

or (Python version)

def foo(a,b):
    c=a+b
    #more code here

Then I need to test these functions by checking the values of the variables in there (within the function scope). I would like to avoid to write print(variables) or return variables every time. Is there any way in that helps me automatically pass all the variables onto the outer scope so the developing environment so that I can easily check them?

Answer

In Julia you can use Base.@locals macro such as:

function foo(a,b)
   c = a+b
   @show Base.@locals
end

Base.@locals returns a Dict with all local variables and I used here @show to show it. Let’s run a test:

julia> foo(10,15)
#= REPL[2]:3 =# Base.@locals() = Dict{Symbol, Any}(:a => 10, :b => 15, :c => 25)
Dict{Symbol, Any} with 3 entries:
  :a => 10
  :b => 15
  :c => 25

In Python there is a very similar locals() function such as:

def foo(a,b):
    c=a+b
    print(locals())

Now let us test it:

>>> foo(10,15)
{'a': 10, 'b': 15, 'c': 25}

If you want rather to export the variables to global scope in Julia you can do:

function foo(a,b)
    c = a+b
    Main.eval.(:($key = $value) for (key,value) in Base.@locals)
end

This copies the values of a,b,c to the Main scope:

julia> foo(200,300);

julia> a,b,c
(200, 300, 500)

Python has no meta-programming but you can just do (suggested by @MisterMiyagi):

globals().update(locals())