The theme is about the scope of the glabal declaration in Python3. I wasted a few hours because the definition part was ambiguous, but I researched various things and learned, so I will keep a record.
This is a code that is very easy to write. It is just a sample.
sample.py
def func():
global x
x = 20
print(f'func x={x}')
def main():
x = 10
func()
print(f'x of main={x}')
if __name__ == '__main__':
main()
I wonder if 20 will be output in both cases.
bash
$ python3 sample1.py
func x=20
x of main=10
why? ?? !! !! Declare global! !! I searched for a few hours, but it was resolved. ↓↓ street.
The declaration with global x
means the following two.
--If there is an object x in the global scope, it can be treated as a ** global variable x ** in the code block (see = same identity). --If there is no object x in the global scope, declare it as a new ** global variable x ** ([This x can be handled from the global scope](https://snowtree-injune.com/2018/12/13/global- nonlocal / # toc6)))
In the previous code, x actually existed in main () (= local scope), not in the global scope. In other words, the global variable x declared in ** func () was completely different from the local variable x in main (). ** **
Declare x in the global scope, not in the local scope called main ().
sample.py
def func():
global x
x = 20
print(f'func x={x}')
def main():
# x =10 This is the local scope
func()
print(f'x of main={x}') #You can refer to global variables (details at the bottom of this article)
if __name__ == '__main__':
x = 10 #Define x in global scope
main()
bash
$ python3 test.py
func x=20
x of main=20
def main
& ʻif name == ‘__ main__’: `If you simply wrote the instruction for the main () part without doing anything, this trouble would not have occurred ...
sample.py
def func():
#global x If you do not declare global here
x = 20
print(f'func x={x}')
def main():
global x
func()
print(f'x of main={x}')
if __name__ == '__main__':
x = 10 #Define x in global scope
main()
bash
$ python3 sample1.py
func x=20
x of main=10
Since I made a global declaration in main (), I wondered if I needed a global declaration in func () called from there, but that wasn't the case.
See the dogma Official Documentation for reasons. It's pretty easy to understand if you read it again.
The global statement is a declarative statement that is maintained throughout the current block of code. The global statement means to specify that the listed identifiers should be interpreted as global variables. It is not possible to assign to a global variable without using global, but you can use a free variable to refer to a global variable without declaring it global.
Does the fact that the declaration is maintained in ** "the entire current code block" **, conversely, has no effect outside the code block?
At the moment when x = 20
is assigned (defined) in func (), it is considered that the local variable x is defined instead of the global variable being rewritten.
sample.py
def func():
global x #func()Global Declaration
x = 20
print(f'func x={x}')
def main():
global x
func()
print(f'x of main={x}')
if __name__ == '__main__':
x = 10 #Define x in global scope
main()
bash
$ python3 test.py
func x=20
x of main=20
If you declare global in both main () and func (), you can treat x in the global scope as a global variable x in the same way.
for your information. You can refer to any object in global scope without declaring global. Of course, it cannot be rewritten. Defined as a local variable the moment you assign a value.
sample.py
def func():
x = 20
print(f'func x={x}')
def main():
func()
print(f'x of main={x}')
if __name__ == '__main__':
x = 10 #Define x in global scope
main()
bash
$ python3 test.py
func x=20
x of main=10
Neither is declared global. Main () can refer to x in global scope because it can only refer to global variables. In func (), the local variable x is defined the moment x = 20
is executed, and it will be treated as a completely different thing from the global variable x in the global scope.
If you do something you are not used to, you will get stuck in various places. Let's do it because you will deepen your learning (I want to believe) while reading the document for the solution.
Below, articles that were easy to understand while studying this time (although links are also provided in the text)
-I tried to examine the questions related to the declaration of global and nonlocal | Snow Tree in June
-
Recommended Posts