Python's scope of variables

Variable scope: refers to a range of variables to be used, in accordance with the scope of the definition of variables, it can be divided into global and local variables

  Global variables: in the general definition of the function or class outer vitro

  Local variables: in the general definition of the function body, and the variable defined in the class, called the class variables or instance variables

Global variables to access, modify

= name " C "   # global variables 
Print ( " My name is: {0} " .format (name))
 DEF ShowLanguageInfo (): 
    Age = 28 # local variable 
    name = " Python "  # value you want to modify global variables , but failed 
    Print ( " my name is: {0}, {1} years old this year, " .format (name, Age)) 
    
ShowLanguageInfo () 
Print ( " my name is: {0} " .format (name ))

 >>> 
my name is: C 
my name is: Python, this year 28 years old 
my name is: C

The use of global variables are declared as global variables

= name " C "   # global variables 
Print ( " My name is: {0} " .format (name))
 DEF ShowLanguageInfo (): 
    Age = 28 # local variables 
    # name = "Python" # want to modify global variables value, but failed 
    global name # declared name as a global variable, using global description 
    name = " Python " 
    Print ( " my name is: {0}, {1} years old this year, " .format (name, Age)) 

ShowLanguageInfo () 
Print ( " my name is: {0} " .format (name))

 >>> 
my name is: C
My name is: Python, this year 28 years old 
my name is: Python

When nested scenarios, how to modify local variables it

= name " C "   # global variables 
Print ( " My name is: {0} " .format (name))
 DEF ShowLanguageInfo (): 
    Age = 28 # local variables 
    # name = "Python" # want to modify global variables value, but failed 
    global name # declared name as a global variable, using global description 
    name = " Python " 
    Print ( " my name is: {0}, {1} years old this year, " .format (name, Age)) 

    DEF setAge (): 
        Age = 18 # want to modify modify local variables of age, 18-year-old is set to forever 
        Print ( "My name is: {0}, {1} years old this year, " .format (name, Age)) 
    
    setAge () # Print Authentication 

    Print ( " My name is: {0}, {1} years old this year " . format (name, Age)) # again to verify that truly successful modification of age, found no 

ShowLanguageInfo () 

Print ( " my name is: {0} " .format (name))

 >>> 
my name is: C 
I the name is: Python, this year 28 years old 
my name is: Python, this year 18 years old 
my name is: Python, this year 28 years old 
my name is: Python

This will be explained using nonlocal variable is outside the local variables, rather than global variables

= name " C "   # global variables 
Print ( " My name is: {0} " .format (name))
 DEF ShowLanguageInfo (): 
    Age = 28 # local variables 
    # name = "Python" # want to modify global variables value, but failed 
    global name # declared name as a global variable, using global description 
    name = " Python " 
    Print ( " my name is: {0}, {1} years old this year, " .format (name, Age)) 

    DEF setAge ():
         # Age = 18 # want to modify modify local variables of age, is set to forever 18 years old 
        nonlocal Age #Statement age as an external local variable, rather than global variables, using nonlocal description 
        age = 18 Print ( " My name is: {0}, {1} years old this year, " .format (name, age)) 
    setAge () # print verify Print ( " my name is: {0}, {1} years old this year, " .format (name, Age)) # again to verify that truly successful modification of age, found success 
ShowLanguageInfo () Print ( " my name is: 0} { " .format (name)) >>> 
my name is: C 
my name is: Python, this year 28 years old 
my name is: Python, this year 18 years old 
my name is: Python, this year 18 years old 
my name is: Python
        
    

    




 

Guess you like

Origin www.cnblogs.com/no-end-to-learning/p/11833885.html