Global and local variables global and local variables deceivers small example

First, we define a local variable, which is displayed in the function Why did not define it? Consider the following analysis

         

Specific code as follows:

Copy the code
# Way: using global strong turn about 
A =. 1 DEF XXX ():
     Print (123 )
     # global A 
    A = A +. 3 Print (A)    # . 4 XXX ()
 Print (A)   # . 4 # Second way: 
A = [. 1 ]
 DEF XXX ():
     Print (123 ) 
    A [0] = A [0] +3
     Print (A)    # [. 4] XXX ()
 Print (A)   # [. 4] # third approach class Foo:
     Pass 
A =

    







 Foo()
a.n = 1
def xxx():
    print(123)
    a.n = a.n +3
    print(a.n)  #4
xxx()
print(a.n)  #4
Copy the code

 

First, we define a local variable, which is displayed in the function Why did not define it? Consider the following analysis

         

Specific code as follows:

Copy the code
# Way: using global strong turn about 
A =. 1 DEF XXX ():
     Print (123 )
     # global A 
    A = A +. 3 Print (A)    # . 4 XXX ()
 Print (A)   # . 4 # Second way: 
A = [. 1 ]
 DEF XXX ():
     Print (123 ) 
    A [0] = A [0] +3
     Print (A)    # [. 4] XXX ()
 Print (A)   # [. 4] # third approach class Foo:
     Pass 
A =

    







 Foo()
a.n = 1
def xxx():
    print(123)
    a.n = a.n +3
    print(a.n)  #4
xxx()
print(a.n)  #4
Copy the code

 

Guess you like

Origin www.cnblogs.com/maaosheng/p/11618799.html