In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail what global variables are defined by keywords within the function. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Global variables can be defined within the function through the global keyword. Global represents the global variable, which is used to change the variables outside the function; the global variable can be created either by an object function or anywhere in the program; the global variable can be referenced by all objects or functions of the program.
Global variables can be defined within the function through the global keyword.
Global is a global variable. When you want to change the variables outside the function, you need to use the global variable global to represent it.
Global variables can be created either by an object function or anywhere in the program. Global variables can be referenced by all objects or functions in this program.
Global usage
For list types: change the first and last letters
NewName = "xiaoming" lst4 = list (newName) def change1 (): lst4 = ['lst4,' change2 () print (lst4)
The results are as follows:
['x','i','a few,'o','m','i','h','g'] ['g','i','a','o','m','i','h','x']
For string types:
Name3 = "xiaoming" def change3 (): name3 = "giaominx" change3 () print (name3) def change4 (): global name3 name3 = "giaominx" change4 () print (name3)
Results:
Xiaominggiaominx
For int:
I = 3def increase (): global i i = 4increase () print (I)
Results: 4
Summary:
1. When you want to change the variables outside the function, you need to declare the function as the global variable global in the function.
2. Lst4 and name3 in change1 and change3 are not variables defined outside the function, but a new function redefined in the function.
The difference between global and this
1. In Python, global refers to a global variable, which represents the same variable only after it is specified (special case: when a variable is a reference data type, it can also represent the same variable when changing its value, such as swap1). Use it when you want to change an external variable.
2. In java, this refers to a member variable, which is already expressed as the same variable without being specified, in order to distinguish between a variable in a function and a member variable when the name of the variable is the same.
3. In comparison, java is more flexible, but it is easy to tamper with data values in the function, so it is necessary to be careful to define member variables. Python does not have to worry about the same naming, but changing global variables is more complicated.
On the function inside the keyword definition of global variables is what is shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.