In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the example analysis of local variables and global variables in python, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.
Four basic types of functions
A local variable is a variable defined within a function [scope is limited to the interior of a function]
Different functions can define the same local variables, but using their own will not have an impact.
The role of local variables: in order to temporarily save data, it needs to be defined in the function for storage.
A variable whose scope is global.
If you want to modify the global variable inside the function, you must declare it using the Global keyword
Local variable def printInfo (): name='peter' # # Local variable, which only acts on the printInfo function. Print ('name') passdef TestMethod (): print (name) pass# TestMethod () printInfo () cannot be used outside the function body.
Pro=' computer Information Management'# A global variable with different scope def printInfo (): name='peter' # # local variable that only acts on printInfo functions. Print ('{}. {} '.format (name,pro)) passdef TestMethod (): name=' Wang Bao' print (name) pass# TestMethod () printInfo () cannot be used outside the function body.
Pro=' computer information management 'name=' Lulu' # when the global variable conflicts with the local variable, the local variable is preferred. When the local variable does not exist, the global variable def printInfo (): name='peter' # # local variable is used, which only acts on the printInfo function Print ('{}. {} '.format (name,pro)) passdef TestMethod () cannot be used outside the function body: name=' Wang Baobao' print (name) pass# TestMethod () printInfo ()
Pro=' computer information management 'name=' Lulu' def printInfo (): name='peter' # # local variable, which only acts on the printInfo function Print ('{}. {} '.format (name,pro)) passdef TestMethod (): name=' Wang Baobao' print (name) passdef changeGlobal ():''to modify the global variable: return:''pro=' Marketing' passchangeGlobal () print (pro) # # check whether it has been modified or not
Pro=' computer information management 'name=' Lulu' def printInfo (): name='peter' # # local variable, which only acts on the printInfo function Print ('{}. {} '.format (name,pro)) passdef TestMethod (): name=' Wang Baobao' print (name) passdef changeGlobal ():''to modify the global variable: return:''global pro pro=' Marketing' passchangeGlobal () print (pro) # # detect whether it has been modified
Thank you for reading this article carefully. I hope the article "sample Analysis of Local and Global variables in python" shared by the editor will be helpful to you. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.