12th Global Variables & local Variables & recursive functions of Python functions

Source: Internet
Author: User

Global variables: At the time of definition, a variable that is written by the head, without any indentation, is a global variable.

Features of global variables: can be invoked anywhere in the current file

Local variables: Variables defined in subroutines are local variables.

Subroutine: For example, a function written in a. py file is a subroutine. and the variables defined in this function are local variables.

Example: Global variables

# global variable name='Alex'  def  change_name ():    Print ('change_name', name)  # a global variable was called Change_name () # Results change_name Alex Print (name)    # The global variable is adjusted # Results Alex

Example: defining local variables in a function body

#Global Variables: global scopeName='Alex'defchange_name (): Name='it's gorgeous.'  #Local Variables: local scope    Print('Change_name', name)#Look in your subroutine first, call the local variable# to give a life example, just get up to find a cell phone, it must now own bedroom (function) to find, his bedroom can not find, it may go to the bedroom (outside the function) to find. # So, well understood, is the priority inside, then outside; local variables can only be used by the function itself (selfish weight), the global variable is more generous, who need to be able to take theChange_name ()#ResultsChange_name's gorgeous.Print(name)#The global variable is adjusted#ResultsAlex

Example: Declaring a global variable in a function body

#Global VariablesName='Alex'defchange_name ():GlobalName#global variables declared in function bodyName='it's gorgeous.'    #The value of the global variable is modified in the function body    Print('Change_name', name)#a global variable was calledChange_name ()#ResultsChange_name's gorgeous.Print(name)#The global variable is changed, but the value of the global variable has been modified#ResultsIt's gorgeous.

A better example

If the contents of the function do not have the global keyword, the local variables are read first, and if there is no local variable, only global variables can be read, and global variables cannot be re-assigned, but for mutable types, internal elements may be manipulated

If the contents of the function have the global keyword, the variable inside the function is a full-board variable that can be read and can be assigned

Name ="Peach Blossom Plum"defYangjian ():GlobalName#It has been declared that name is the global variable .    Print('I'm going to', name) name="Little Northeast"  #Modifying a global variable    Print('I'm going to', name)defQupengfei (): Name="Base"    Print('I'm doing', name) Yangjian () Qupengfei ()#ResultsI want a peach blossom, I want a little northeast, I want a base .

Summarize

Global variable name capitalization
Local variable variable name lowercase
The  following example uses uppercase to illustrate the relationship between a global variable and a local variable.

# If the contents of the function have no global keyword:
#-There are declared local variablesNAME = ["Product Manager","Riopo Wet"]defQupengfei (): NAME="own" Print('I'm doing', NAME) Qupengfei ()#-No declaration of local variablesNAME = ["Product Manager","Riopo Wet"]defQupengfei (): Name.append ('Xxoo') Print('I'm doing', NAME) Qupengfei () # If the contents of the function have the Global keyword #-there is a declared local variableNAME = ["Product Manager","Riopo Wet"]defQupengfei ():GlobalName Name="own" Print('I'm doing', NAME) Qupengfei ()#The error example, all variables are placed under the local variable, is not possible, so if you want any global variables, try to put forward. NAME = ["Product Manager","Riopo Wet"]defQupengfei (): NAME="own" GlobalNAMEPrint('I'm doing', NAME) Qupengfei ()#-No declaration of local variablesNAME = ["Product Manager","Riopo Wet"]defQupengfei ():GlobalName Name= ["Mao"] Name.append ('Xxoo') Print('I'm doing', NAME) Qupengfei ()

Functions can also be nested within a function, as shown in the order of execution

Name='Sea Breeze'defHuangwei (): Name="Huang Wei"    Print(name)defLiuyang (): Name="Liu Yang"        Print(name)defNulige (): Name='Furnace Finger Flower'            Print(name)Print(name) Nulige () Liuyang ()Print(name) Huangwei ()#ResultsHuang Wei Liu Yang Liu Yangyi finger Flower Huang Wei

Name ="just mother ."defWeihou (): Name="Chen Zhuo"    defWeiweihou ():GlobalName#Global, which declares a globals variable,Name ="Calm down"  #The outermost global variable is modified here, not a variable of the same name above itWeiweihou ()Print(name)Print(name) Weihou ()Print(name)#Resultsjust Niang Chen Zhuo calm

Name ="just mother ."defWeihou (): Name="Chen Zhuo"    defWeiweihou (): nonlocal name#nonlocal, specify the upper-level variable, and if not, continue up until you find it .Name ="Calm down"Weiweihou ()Print(name)Print(name) Weihou ()Print(name)#Resultsjust Niang calm just Niang

12th Global Variables & local Variables & recursive functions of Python functions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.