-1

Here is a bare-bones example of my code:

def function():
  var_a = 1
  var_b = 2
  var_c = 3
  return var_a, var_b, var_c

function()

(Apologies for wonky formatting). Basically, the function takes in no arguments and its single job is variable assignment. I need to be able to call on the function for quick assignment and then be able to use those variables in the rest of my code. The problem is that I can't get those variables out of the function.

So if I use the above code and put a print(var_a) line after, then it will tell me that var_a is not defined. If I say values = function() , print (values) then I will get only the values printed in a list, but I won't be able to access them in the way I want. Is there an easy way to fix this?

  • When you run the function, why are you not saving the returned values? `var_a, var_b, var_c = function()` – Yoshikage Kira Jun 10 '21 at 17:39
  • 1
    Stack Overflow is not intended to replace existing documentation and tutorials. Repeat your tutorial on functions to learn how to use the values you return to the main program. – Prune Jun 10 '21 at 17:39
  • Are you expecting global variables to be assigned/created by calling `function`? You have to declare each variable as global with something like `global var_a, var_b, var_c` at the beginning of the function to avoid creating local variables. – chepner Jun 10 '21 at 17:42

1 Answers1

3

You need to assign the values that you return from the function.

a, b, c = function()

If you assign the three values to a single tuple, you can also destructure them from the tuple the same way:

values = function()
a, b, c = values
Samwise
  • 51,883
  • 3
  • 26
  • 36