In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
For many beginners, it is easy to confuse global and local variables. I believe you should understand if you take a look at the explanation below.
The difference between the two.
Definition:
Global variables: inside the module, outside all functions, outside the class
Local variables: within the function, within the class method
Let's take a look at the example
Global variables are called inside the function
A = "hello" # Global variable a
Def test ():
Global a # calls the global variable a
When an is called later in the b = a # test method, it is all global a
Print (bmaine a)
Test ()
Call the global variable an in the test function to see the result of the run
After running, the values of global variables are hello.
The function uses a local variable with the same name as the global variable
A = "hello" # Global variable a
Def test ():
A = "hell0 local" # defines a local variable a
When an is called later in the b = a # test method, it is all local a
Print (b + ",", a)
Test ()
Here, another an is defined in the function test, which is a local variable, and then the a called in test is all local a. Take a look at the running results:
Modify the global variable value within the function
A = "hello" # Global variable a
Def test ():
Global a
A = "hell0 global" # modify the value of the global variable a
When an is called after b = a # test method, it is all global a
Print (b + ",", a)
Test ()
In the function test, the global an is declared first, and then the an is modified, which is tantamount to changing the value of the global variable a.
Take a look at the running results:
Note: the variable inside the method is before the = sign, which must be a local variable. If it appears after the = sign for the first time
On the other hand, it must be the called global variable; the global variable can be called in the function, and the local variable can only be called in the corresponding function.
It is called within the number, and cannot be called anywhere outside the function.
Add QQ × × stream 610845268 if there is a problem.
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.