Write at the front:
The biggest difference between global and local variables in Python is that local variables can only be accessed by functions, whereas global variables can be accessed directly.
!!!!!!!!!
Let’s start with the simplest example where this is not really a global variable, just a variable in a class.
!!!!!!!!!
So let’s start by talking about access to a variable in a class so how do we access that variable?
class variable:
a = 'I'm a class variable'
def showvarible(self):
b = I'm a function variable.
print(a)
print(b)
variable().showvarible()Copy the code
No doubt, the compiler has already reported an error because variables in a class cannot be accessed directly from a function, as they should be
class variable:
a = 'I'm a class variable'
def showvarible(self):
b = I'm a function variable.
print(variable.a)
print(b)
variable().showvarible()Copy the code
I'm a class variable and I'm a function variableCopy the code
We can actually access it by self
class variable:
a = 'I'm a class variable'
def showvarible(self):
b = I'm a function variable.
print(self.a)
print(b)
variable().showvarible()Copy the code
The result is the same
I'm a class variable and I'm a function variableCopy the code
What else can we do, we give a constructor an argument that is accessible in the class
class variable:
def __init__(self,a):
self.a = 'I'm a class variable'
def showvarible(self):
b = I'm a function variable.
print(self.a)
print(b)
variable(1).showvarible()Copy the code
I'm a class variable and I'm a function variableCopy the code
The important thing to note here is that you have to give an argument when you instantiate it, and since Python is a dynamic language, you don’t need to specify the type of the argument, so you can put ints, like 1, or you can give a string.
Then let’s really look at global variables
a = 'I'm a real global variable'
def showvariable():
b = 'I'm always a local variable.'
print(a)
print(b)
showvariable()Copy the code
I'm really a global variable and I've always been a local variableCopy the code
When we try to access a local variable of a function outside the function
a = 'I'm a real global variable'
def showvariable():
b = 'I'm always a local variable.'
print(a)
def showb():
print(b)
showvariable()Copy the code
An error
I am the real global variable Traceback (most recent call last): File"F:/leetcode/xxx.py", line 13, in <module>
showb()
File "F:/leetcode/xxx.py", line 9, in showb
print(b)
NameError: name 'b' is not definedCopy the code
Local variables can only be used locally.
Unfortunately, my local variable and my global variable have the same name. Which one has higher priority?
a = 'I'm a real global variable'
def showvariable():
a = 'I'm always a local variable.'
print(a)
print(a)
showvariable()Copy the code
Sure enough, the function uses local variables in preference
Another example of prioritization is the following
a = 3
def showvariable():
a = a * 5
print(a)
showvariable()Copy the code
You might expect the output to be 15, but the example above shows that local variables are preferred within functions
3
Traceback (most recent call last):
File "F:/leetcode/xxx.py", line 10, in <module>
showvariable()
File "F:/leetcode/xxx.py", line 5, in showvariable
a = a * 3
UnboundLocalError: local variable 'a' referenced before assignmentCopy the code
This is because we defined a local variable a in the function, but before we can assign the value, the compiler does not know who to use to *5, of course, error.
Let’s talk about global. For the first time, global can only be defined and can’t be assigned
def showvariable():
global a
a = 'I am a global'
print(a)
showvariable()Copy the code
I am a globalCopy the code
Of course, it can be accessed outside the function
def showvariable():
global a
a = 'I am a global'
print(a)
def showglobal():
print(a)
showvariable()
showglobal()Copy the code
Copy the code
I'm Global. I'm GlobalCopy the code
But you have to assign it to make sense if it looks like this
def showvariable():
global a
a = 'I am a global'
print(a)
def showglobal():
print(a)
print(a)
showvariable()
showglobal()Copy the code
When no value is assigned, print
Traceback (most recent call last):
File "F:/leetcode/xxx.py", line 9, in <module>
print(a)
NameError: name 'a' is not definedCopy the code
Since global A has not been assigned at this point, it cannot be printed.