IE which is better:
CONSTANT = (1, 3, 5, 8) # #SOME OTHER CODE HERE def function1(arg): if arg in CONSTANT: do something
or:
def function1(arg):
CONSTANT = (1, 3, 5, 8)
if arg in CONSTANT:
do something
Global vs. Local Variables and Namespaces,The following example shows a wild combination of local and global variables and function parameters:,DE Globale und lokale Variablen und Namensräume in Python,Python3 introduced nonlocal variables as a new kind of variables. nonlocal variables have a lot in common with global variables. One difference to global variables lies in the fact that it is not possible to change variables from the module scope, i.e. variables which are not defined inside of a function, by using the nonlocal statement. We show this in the two following examples:
def f():
print(s)
s = "I love Paris in the summer!"
f()
I love Paris in the summer!
def f():
s = "I love London!"
print(s)
s = "I love Paris!"
f()
print(s)
I love London!
I love Paris!
def f():
print(s)
s = "I love London!"
print(s)
s = "I love Paris!"
f()
---------------------------------------------------------------------------
UnboundLocalError Traceback (most recent call last)
<ipython-input-3-d7a23bc83c27> in <module>
5
6 s = "I love Paris!"
----> 7 f()
<ipython-input-3-d7a23bc83c27> in f()
1 def f():
----> 2 print(s)
3 s = "I love London!"
4 print(s)
5
UnboundLocalError: local variable 's' referenced before assignment
Last Updated : 15 Jul, 2022
I love Geeksforgeeks
I love Geeksforgeeks
Output:
NameError: name 's'
is not defined
Inside Function I love Geeksforgeeks
Outside Function I love Geeksforgeeks
Me too.
I love Geeksforgeeks
Python is great!GFG
Look
for Geeksforgeeks Python Section
Look
for Geeksforgeeks Python Section
That said, using the same variable name for global and local variables is not considered a best practice. Make sure that your variables don't have the same name, as you may get some confusing results when you run your program.,Variable scope refers to the parts and boundaries of a Python program where a variable is available, accessible, and visible.,When you define a variable outside a function, like at the top of the file, it has a global scope and it is known as a global variable.,For the rest of this article, you will focus on learning about creating variables with global scope, and you will understand the difference between the local and global variable scopes.
So, if you wanted to store your age which is an integer, or int
type, this is what you would have to do in C:
#include <stdio.h>
int main(void)
{
int age = 28;
// 'int' is the data type
// 'age' is the name
// 'age' is capable of holding integer values
// positive/negative whole numbers or 0
// '=' is the assignment operator
// '28' is the value
}
However, this is how you would write the above in Python:
age = 28 # 'age' is the variable name, or identifier # '=' is the assignment operator # '28' is the value assigned to the variable, so '28' is the value of 'age'
Keep in mind that you can change the values of variables throughout the life of a program:
my_age = 28
print(f "My age in 2022 is {my_age}.")
my_age = 29
print(f "My age in 2023 will be {my_age}.")
#output
#My age in 2022 is 28.
#My age in 2023 will be 29.
Look at what happens when I try to access that variable with a local scope from outside the function's body:
def learn_to_code():
#create local variable
coding_website = "freeCodeCamp"
print(f "The best place to learn to code is with {coding_website}!")
#try to print local variable 'coding_website'
from outside the
function
print(coding_website)
#output
#NameError: name 'coding_website'
is not defined
You can use it inside a function's body, as well as access it from outside a function:
#create a global variable
coding_website = "freeCodeCamp"
def learn_to_code():
#access the variable 'coding_website'
inside the
function
print(f "The best place to learn to code is with {coding_website}!")
#call the
function
learn_to_code()
#access the variable 'coding_website'
from outside the
function
print(coding_website)
#output
#The best place to learn to code is with freeCodeCamp!
#freeCodeCamp
What are the “best practices” for using import in a module?,Why are default values shared between objects?,How do I share global variables across modules?,You could use a global variable containing a dictionary instead of the default value; it’s a matter of taste.
>>> x = 10 >>> def bar(): ...print(x) >>> bar() 10
>>> x = 10 >>> def foo(): ...print(x) ...x += 1
>>> foo()
Traceback(most recent call last):
...
UnboundLocalError: local variable 'x'
referenced before assignment
>>> x = 10 >>>
def foobar():
...global x
...print(x)
...x += 1 >>>
foobar()
10
>>> print(x) 11
>>> def foo():
...x = 10
...def bar():
...nonlocal x
...print(x)
...x += 1
...bar()
...print(x) >>>
foo()
10
11