In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how python defines and uses global variables". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
First of all, we need to know what is a global variable and what is a local variable. Global variables, which refer to variables that can be called by all functions, are generally at the outermost layer of the code block. On the contrary, the local variable can be called within the current function, and the local variable will be destroyed immediately after jumping out of the function and cannot be called.
Num = 1 # this is a global variable in order to be beautiful, I put it on the first line def update (): # example function y = 1 # this is a local variable, only this function can use print (num) # global variable can be referenced inside the function y = num+1 # global variable can participate in the operation return y # function ends, y will be destroyed Yupdate () # print (y) # cannot be referenced externally because y is a local variable within the function and will be destroyed at the end of the function, so it cannot be referenced by yprint (num) # global variables.
As shown above, as long as a variable is defined at the outermost layer, the variable can be referenced globally. Then a question arises: is it feasible if I want to change the value of this global variable within the function?
The answer is feasible, but it is necessary to master certain methods.
Int main () {printf ("Hello, World!\ n% d", a); a = printf ("\ n% d", a); test (); printf ("\ n% d", a); return 0;}
As shown in the above code, the value of the global variable in C language can be modified directly, so can python be modified directly like this? That's right. No. If you modify it directly like the C language, the code will report an error (unresolved references). At this point, the role of the global keyword appears. If you want to modify the value of a global variable within the function, you can change the value of the global variable within the function as long as you declare the global variable with global inside the function.
Num = 1 # this is a global variable in order to be beautiful, I put it on the first line def update (): # example function global num # declares num using global. In the function, you can change the value of the global variable num + = 1 # Global variable can participate in the operation return 0print (num) # check the global variable update () # call the function Modify the global variable print (num) # after calling the function to see the global variable "how python defines and uses global variables", thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.