-5

Is this can be ignored. I am getting warning as described below. How will my code succeed? I have to have this ignoreList variable in my program in global scope at whatever the cause. And also I don't know why it doesn't print else: block print statement.

try:
    ignoreList
except NameError:
    global ignoreList
else:
    print 'If property of ignoreList is not set, then please adjust properties to be set for ignoreList'    

Here is the Warning while in the execution of program in python IDLE

Warning (from warnings module): File "C:\Users\Sathasivam_Anand\Documents\ignore_list_check.py", line 4 global ignoreList SyntaxWarning: name 'ignoreList' is used prior to global declaration

>>> ===== RESTART: C:\Users\Sathasivam_Anand\Documents\ignore_list_check.py =====

Dev Anand Sadasivam
  • 699
  • 7
  • 21
  • 49

1 Answers1

1

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.

Community
  • 1
  • 1
Seth
  • 1,215
  • 15
  • 35
  • 1
    I got failure when I tried something similar like this ,- `global a = 10`. Are you sure that this the correct way. – Dev Anand Sadasivam Jan 18 '17 at 08:01
  • 1
    @DevAnandSadasivam: `global a = 10` is wrong: you can't do an assignment in a `global` statement. But Seth is correct in saying that a `global` statement in the global context (i.e. outside a function or class definition) has no effect. And generally, you really should try to avoid having modifiable globals in your code because they make the code non-modular (which makes it harder to test, maintain, & debug), there's almost always a better way. – PM 2Ring Jan 18 '17 at 08:22
  • 1
    Sorry for that error. By splitting it in two lines it works, I fixed the example accordingly. Still, the real question is why you would only make that variable available in the global scope if there is an error? That seems/feels rather uncommon and as PM 2Ring pointed out there is probably a cleaner way to do whatever you intend by doing it that way. The linked question also has some information on why it's a bad idea to use globals. – Seth Jan 18 '17 at 08:29