2

I have a Python file that have the following lines:

import sys

global AdminConfig
global AdminApp

This script runs on Jython. I understand the use of global keyword inside functions, but what does the use of the "global" keyword on module level mean?

ᄂ ᄀ
  • 5,669
  • 6
  • 43
  • 57
tt0686
  • 1,771
  • 6
  • 31
  • 60
  • This post might be useful for you http://stackoverflow.com/questions/4693120/use-of-global-keyword-in-python – AtAFork Nov 21 '14 at 17:02
  • What does it mean? Nothing at all. Whoever wrote this code was ignorant or mistaken about what the keyword does. – Daniel Roseman Nov 21 '14 at 17:04
  • Those objects "AdminConfig" and "AdminApp" are implemented by the Webpshere Application Server and this file use them , another question that i have is how they are fullfilled ? The only import is the sys module – tt0686 Nov 21 '14 at 17:08

1 Answers1

1

global x changes the scoping rules for x in current scope to module level, so when x is already at the module level, it serves no purpose.

To clarify:

>>> def f(): # uses global xyz
...  global xyz
...  xyz = 23
... 
>>> 'xyz' in globals()
False
>>> f()
>>> 'xyz' in globals()
True

while

>>> def f2():
...  baz = 1337 # not global
... 
>>> 'baz' in globals()
False
>>> f2() # baz will still be not in globals()
>>> 'baz' in globals()
False

but

>>> 'foobar' in globals()
False
>>> foobar = 42 # no need for global keyword here, we're on module level
>>> 'foobar' in globals()
True

and

>>> global x # makes no sense, because x is already global IN CURRENT SCOPE
>>> x=1
>>> def f3():
...  x = 5 # this is local x, global property is not inherited or something
... 
>>> f3() # won't change global x
>>> x # this is global x again
1
ch3ka
  • 11,792
  • 4
  • 31
  • 28