What is the Python equivalent of getattr () in Julia? I tried the following metaprogramming code, but it only works in the global area, not inside the function area.
type A name value end a = A("Alex",1) for field in fieldnames(a) println(eval(:(a.$field))) end
This will print:
Alex 1
However, if the above is within the scope of the function, then this will not work
function tmp() a = A("Alex",1) for field in fieldnames(a) println(eval(:(a.$field))) end end tmp()
Error:
ERROR: LoadError: UndefVarError: a not defined
EDIT: Thanks to everyone for answering the question. Here are links to Julia documentation on getfield and setfield! .
source share