Functions are added to the current namespace like any other name would be added. That means you can use the global keyword inside a function or method:
Copydef create_global_function():
global foo
def foo(): return 'bar'
The same applies to a class body or method:
Copyclass ClassWithGlobalFunction:
global spam
def spam(): return 'eggs'
def method(self):
global monty
def monty(): return 'python'
with the difference that spam will be defined immediately as top-level class bodies are executed on import.
Like all uses of global you probably want to rethink the problem and find another way to solve it. You could return the function so created instead, for example.
Demo:
Copy>>> def create_global_function():
... global foo
... def foo(): return 'bar'
...
>>> foo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'foo' is not defined
>>> create_global_function()
>>> foo
<function foo at 0x102a0c7d0>
>>> foo()
'bar'
>>> class ClassWithGlobalFunction:
... global spam
... def spam(): return 'eggs'
... def method(self):
... global monty
... def monty(): return 'python'
...
>>> spam
<function spam at 0x102a0cb18>
>>> spam()
'eggs'
>>> monty
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'monty' is not defined
>>> ClassWithGlobalFunction().method()
>>> monty()
'python'
Answer from Martijn Pieters on Stack OverflowFunctions are added to the current namespace like any other name would be added. That means you can use the global keyword inside a function or method:
Copydef create_global_function():
global foo
def foo(): return 'bar'
The same applies to a class body or method:
Copyclass ClassWithGlobalFunction:
global spam
def spam(): return 'eggs'
def method(self):
global monty
def monty(): return 'python'
with the difference that spam will be defined immediately as top-level class bodies are executed on import.
Like all uses of global you probably want to rethink the problem and find another way to solve it. You could return the function so created instead, for example.
Demo:
Copy>>> def create_global_function():
... global foo
... def foo(): return 'bar'
...
>>> foo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'foo' is not defined
>>> create_global_function()
>>> foo
<function foo at 0x102a0c7d0>
>>> foo()
'bar'
>>> class ClassWithGlobalFunction:
... global spam
... def spam(): return 'eggs'
... def method(self):
... global monty
... def monty(): return 'python'
...
>>> spam
<function spam at 0x102a0cb18>
>>> spam()
'eggs'
>>> monty
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'monty' is not defined
>>> ClassWithGlobalFunction().method()
>>> monty()
'python'
You can use global to declare a global function from within a class. The problem with doing that is you can not use it with a class scope so might as well declare it outside the class.
Copyclass X:
global d
def d():
print 'I might be defined in a class, but I\'m global'
>> X.d
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'X' object has no attribute 'd'
>> d()
I might be defined in a class, but I'm global
Why would one define "global variables" in python functions? How to use these correctly?
How to make variables global by default, Python 3.11
Why can't imported functions access global variables?
Question about AWS Lambda, python, and globals
The code that sets the global variable will be evaluated once per Execution Context when your module is imported. After that, the value of the variable is in memory and there's no need to calculate it again.
More on reddit.comVideos
I am looking at some legacy code whereby the author often defines global variables within the functions with global. I have rarely seen this done, and I'm not sure it's been done correctly.
Let's take a concrete example: https://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them
globvariable = 0
def maketheglobalvariable1():
global globalvariable
globvariable = 1
def printglobvariable():
print(globvariable)
maketheglobalvariable1()Here's it's clear that a variable has been defined outside any functions, and then a global variable was defined within a function in over to overwrite it when the function is called.
But when would there be a time to define global variables in python? These seem to be superfluous, no?
Any examples/resources to better help me understand this topic are greatly appreciated :)