Access variable inside of a funciton from another function


hello, everyone, I have a question about how to access local variable of a funciton in another funciton, my code is like this:

function FA()
    name = "world"
    return "hello"
function FB(f::Function)


get this error:

ERROR: type #FA has no field name

so how to get variable name of FA in FB ?

thanks for any help~


The short answer is: you can’t. But perhaps if you can explain why you want to access that information we can help you find another way.


I am trying to implement some basic features of python-fire package( with julia. My current work result is, Jfire can call modules or functions, but still cann’t output more help information about funciton parameters.

assume you have a file myth.jl is like this:

using Jfire

module myth
export hello
function hello(;name::String="sikaiwei", greet::String="how is the weather?", number::Number=3)
	println("hello, $name. $greet. $number")

if abspath(PROGRAM_FILE) == @__FILE__
	Jfire.Fire(myth, time=true, color=:yellow, info=false)

so you can call help information like:

julia myth.jl hello -h

will output

Module myth
Function hello:

Keyword Arguments:(optional)

but I want to output more detail description about --name/–greet/–number(example:

 --name some_detail_description 

so at first I try to defined these description in Function hello, then maybe I call access these description in Jfire.Fire.


I am curious, there is some language that you know that allows this kind of construct? I do not know any language that allow this.

You could have all variables of the function A inside a dict (or another structure) that is returned by the function A and used by function B. Or use a global variable shared by both (what is terrible for performance in Julia). Or function A is a (possibly global) object with many fields and that is callable. Really depends on what is your objective here.


i think javascript allows this


yes, some filed that is callable is a good choice, which filed I can use ?
A is just a julia Function


I am almost sure Javascript does not allow this, unless you are referring to implicit globals.


If I understand correctly the purpose of the Python Fire library that you are trying to replicate, it does not seem to look at variables inside other functions, but instead it uses reflection to inspect the list of parameters of a method, print them to the user, and interpret command line arguments to call some registered function and pass the arguments to it.

If you want to do more (print extra info on the parameters that is not readily available by reflexivity), maybe you could establish some convention, as that extra info is stored in a const global with the name of the function plus a suffix, and check if it is defined when you register a function. If it is, you use the information provided there to complement your output. I am not sure however, how this would work with Julia allowing multiple definitions of the same function. Python allows function overload by the method signature? How PythonFire deals with that?

Someone with more experience than me could say if this a good idea.

1 Like