global variable warning in python

Viewed 74975

I have a python 2.6 script (yes I know I should upgrade to at least 2.7) that looks like this:

ret_code = 0

def some_func()
  global ret_code
  ...

if __name__ == '__main__':
  global ret_code
  ...

Now I get a warning if I run the code: *SyntaxWarning: name 'ret_code' is assigned to before global declaration global ret_code*

Why do I get this warning?

I can solve the problem by doing so:

def some_func()
      global ret_code
      ...

if __name__ == '__main__':
  global ret_code
  ret_code = 0 #assign 0 here instead of above
  ...

Still that doesn't answer my question. What is wrong with the original code?

1 Answers
Related