[Solved] Warning on Python global variable declaration


Your code fragment doesn’t make a lot of sense without more context. To get a better understanding of the keyword look at this: Use of “global” keyword in Python

As for your code it makes the variable only available in a global scope after it errors which is unusual. As the warning indicates you would/should define the variable ignoreList a global from the start to get rid of the error. The question would be why you would only expose it if the code runs into an error to begin with.

Furthermore if you didn’t include it in some kind of function or other encapsulation the global keyword doesn’t do anything int hat context.

As an example for a scenario where you would need to use global in order to expose a variable in another scope:

def test():
    global a
    a = 10
    return 20
b = test()
print(a,b)

An example where it doesn’t make sense as there is just a single scope to begin with:

a = 10
global a
b = 20
print(a,b)

Your code fragment would indicate this case as you’re missing additional indention. You might have omitted it purposely but by also omitting any information about the code that surrounds it (e.g. if it is placed within a function) you code doesn’t make a lot of sense.

3

solved Warning on Python global variable declaration