Do you guys really tell me to transfer a variable from a function to another python function? Example:

def Func1(){ var1 = 'data' } def Func2(){ print (var1) } 

Is it possible to implement such a thing as something without global variables?

  • 3
    Does returning a value from a function through return do not suit you for some reason? - Vladimir Martyanov
  • Not suitable, because the function already returns a value ... - user238428
  • one
    @ user238428 you can return tuple: just return a comma-separated value - jfs

1 answer 1

To pass an argument to another function, it must at least take it. You can call the second from the first function with the desired argument.

 def Func1(): var1 = 'data' Func2(var1) def Func2(var1): print(var1) Func1() 

The first function can also return a value using return. This value can be immediately transferred to the call of the second function without creating global variables.

 def Func1(): var1 = 'data' return var1 def Func2(var1): print(var1) Func2(Func1()) 

PS In Python, indentation is used instead of operator brackets.