Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of Local variables and Global variables in python

Shulou Source: shulou.com Published: 2022-06-01 07:57:46 09月16日 Update

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!

Tags: Variable function local global function information article computer insurance management example analysis different market marketing detection marketing same value key Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Docker Apple OPPO Reno Shulou Tech Info