-1

I made the following sample to change one of the global variables.

a1 = 0
a2 = 0
a3 = 0
a4 = 0  #and more global variables

def foo():
    global a3
    if a3 < 1:
        a3 = 1

foo()
print a3   #is 1

But I don't want to create a function for each global variable. So, I made the following one.

a1 = 0
a2 = 0
a3 = 0
a4 = 0  #and more global variables

def foo(name):
    global name
    if name < 1:
        name = 1

foo(a3)
print a3   #expect 1

And I got an error message.

SyntaxError: name 'name' is local and global

I don't know how to resolve the issue. Please help.

  • 5
    Don't use four separate globals in the first place: use a `list` or a `dict` with 4 entries, and pass an index to your function to update the correct one. – chepner May 22 '21 at 22:50
  • @BuddyBob They don't want to pass the value in the variable; they want to pass the *name* of the variable. – chepner May 22 '21 at 22:50
  • Yeah, I just realized – Buddy Bob May 22 '21 at 22:50
  • 2
    That means it should be a dict, not four variables. (Technically the dict already exists as `globals()`, but don't do that.) – Samwise May 22 '21 at 22:58
  • 1
    Don't do this. Functions shouldn't be modifying global variables *to begin with*, and you *defintely* don't want a function to dynamically modifying some global variable. Change your design – juanpa.arrivillaga May 22 '21 at 23:03
  • 1
    In addition to the other comments, you should be using Python 3 instead of Python 2 as you are currently running. (If you're curious how I can tell how you're using Python 2, the `print` statement gives it away. In Python 3 you use `print(value)`.) Python 2 is obsolete and unsupported. Uninstall it and download the latest Python 3 (currently 3.9.5) from [python.org](https://www.python.org/downloads/). – Michael Geary May 22 '21 at 23:21
  • Thank you guys for the suggestions ;)) – tatsuya tanaka May 22 '21 at 23:24
  • Also, if you're not already using a good Python debugger, you will find it extremely helpful to learn and use one. [PyCharm Community Edition](https://www.jetbrains.com/pycharm/) is free and includes an excellent debugger that lets you step through your code line by line and see exactly what is going on in each step. I've been programming in Python for a long time and use the PyCharm debugger every day. – Michael Geary May 22 '21 at 23:27

2 Answers2

0

You need to resolve the issue by fixing your program design. What you're doing is bad practice. Look up the many, many warnings against global variables.

You also don't seem to grasp the difference between a variable's name and the concept it represents. Don't worry; you'll get that with more practice. What you're trying to do here is simply to change the value of a variable shared by the various program modules. One way to do this is to use a mutable data structure, and pass it around properly. For instance:

def foo(a_values, pos):
    if a_values[pos] < 1:
        a_values[pos] = 1

a = [0, 0, 0, 0]
foo(a, 3)
print(a[3])

Also see Variable number of variables.

Prune
  • 76,765
  • 14
  • 60
  • 81
0

You can either use a dictionary:

variables = {
"a1": 0,
"a2": 0,
"a3": 0,
"a4": 0 # add as many as you want
};

def foo(name):
    global variables
    if variables[name] < 1:
        variables[name] = 1
foo("a3")
print variables["a3"]  # output should be 1

or a List

a = [0, 0, 0, 0] # add as many as you want


def foo(index):
    global a
    if a[index] < 1:
        a[index] = 1

foo(2)
print a[2]  # output should be 1
pssngr
  • 21
  • 7