It is not possible directly for a very simple reason, prueba
does not return "nothing" , the function is limited to printing the string by the standard output and returning None
(all function / method returns None
by default). Just like if you do:
>>> var = prueba()
var
is None
, which is what the function returns and not "hola mundo"
.
On the other hand, exec
always returns None
, discarding any return resulting from executing the received code, but you can make an assignment in the chain that you pass without problems, as long as it is code Python valid exec
does the "magic", very dark and dangerous indeed ...:)
def prueba():
return("hola mundo")
exec("var = prueba()")
print(var)
or use eval
, which only allows an expression (if what is passed is a string) unlike exec
but, on the other hand, returns the result of its evaluation:
def prueba():
return("hola mundo")
var = eval("prueba()")
print(var)