[0809 | Day 12] nested object variable-length parameters / functions / function / namespace and scope

Vararg

One, Katachisan

Location parameter

The default parameter

Second, the argument

Argument position

Keyword argument

Third, the variable-length parameters *

def func(name,pwd,*args):
    print('name:',name,'pwd:',pwd)
    print(args)
    return 1
​
res = func('nick',12345,18,180,18000)
print(res)
​
#name: nick pwd: 12345
#(18, 180, 18000)
#1

 

Fourth, the variable-length parameters **

def func(name,pwd,**kwargs):
    print('name:',name,'pwd:',pwd)
    print(kwargs)
    return 1
​
res = func('nick',12345,age = 18,height = 180,wage = 18000)
print(res)
​
name: nick pwd: 12345
{'age': 18, 'height': 180, 'wage': 18000}
1

 

Five, * tuple applications

DEF FUNC (name, pwd, X, Y, Z):
     Print ( ' name: ' , name, ' pwd: ' , pwd)
     Print (X, Y, Z)
     return . 1 
tuple = (l, 2,3 ) 
RES = FUNC ( ' Nick ' , 12345, tuple *)   # the tuple broken into a position to pass the position parameter argument Print (RES) 
name: Nick pwd: 12345 
. 1 2. 3 
. 1

 

Six, ** dict applications

DEF FUNC (name, pwd, X, Y, Z):
     Print ( ' name: ' , name, ' pwd: ' , pwd)
     Print (X, Y, Z)   # X, Y, Z must be a dictionary key a one correspondence, if dict = { 'a': 4 , 'b': 5, 'c': 6}, then Print (a, B, C) 
    return . 1 
dict = { ' X ' :. 4, ' Y ' :. 5, ' Z ' :. 6 } 
RES = FUNC ( ' Nick ' , 12345, ** dict)   # will be broken into dict keyword position parameter argument passed Print (RES) 
name: Nick pwd:
12345
4 5 6
1

 

Seven, on application parameters

# Example a 
DEF FUNC (X, Y, F = 0, * args, ** kwargs):
     Print (F ' F = ' , F)
     Print (args)
     Print (kwargs)
     return X + Y 
RES = FUNC (. 1 , 2,. 3, A =. 4, B =. 5, C =. 6)   # correspondence, then the default parameter f = 0 argument receiving position 3, args may not receive a value Print (RES) 
F =. 3 
( ) 
{ ' A ' :. 4, ' B ' :. 5, ' C ' :. 6 }
 . 3 # Example two


DEF FUNC (X, Y, F = 0, * args, ** kwargs):
     Print (F ' F = ' , F)
     Print (args)
     Print (kwargs)
     return X + Y 
RES = FUNC (. 1, 2, =. 4 A, B =. 5, C =. 6)    # correspondence, then the default parameter f = 0 does not correspond to the received, default is 0, args may not receive a value 
Print (RES) 
F = 0 
() 
{ ' A ' :. 4, ' B ' :. 5, ' C ' :. 6 }
 . 3 # Example three

DEF FUNC (X, Y, F = 0, * args, ** kwargs):
     Print (F ' F = ' , F)
     Print (args)
     Print (kwargs)
     return X + Y 
RES = FUNC (. 1, Y = 2 , =. 4 A, B =. 5, C =. 6)     # keyword argument y = 2 will find their own key y, 1 is the one-argument position 
# RES = FUNC (y = 2,. 1, A =. 4 , b = 5, c = 6 ) # correspondence, keyword argument must be placed behind the position of the argument, or y = 2 is assigned to y, corresponds. 1 is y, then error 
Print (RES) 
F = 0 
() 
{ ' A ' :. 4, ' B ' :. 5, ' C ': 6}
3

 

 

Object Functions

  1. Quote

  2. As a function of the parameters

  3. As the return value of the function

  4. As the container element

Nested functions

def f1():
    def f2():
        pass
f1()

 

 

Namespace and scope

Namespaces

Built-in namespace

Python interpreter to generate the start time, as len / int / dict

Global name space

When executing file generation

The local name space

When a function call is generated

Execution order

Built----> Global ---> Local

Find the order

Current Location ---> local ---> Global ---> Built

# Search order: Position -> local -> Global -> Built- 
X. 1 = DEF F1 (): 
    X = 2
     Print (X)   # current position results: 2 F1 () 
X =. 1
 Print (X)   # global results:. 1 DEF F1 (): 
    X = 2 
F1 () 
X =. 1
 DEF F1 (): 
    X = 2 
F1 () Print (X) # global results:. 1 
X =. 1
 DEF F1 ():
     Print (x) # local no value of x, go global search results: 1 
f1 ()




 

Scope

Range have an effect

Global scope

+ Built-in global name space variables

. 1 x =
 DEF F1 (): 
    x = 3 
F1 () 
Print (x)   
# . 1     
x = x =. 1 and no relationship between the two x 3   
 

Local scope

Variable local name space, different functions have different scopes

DEF F1 (): 
    X =. 1
     DEF F2 (): 
        X =. 3
     Print (X) 
F1 () 
# . 1 
x =. 1 x 3 and x = two no relation

 

Note: The global scope and local scope noninterference in each other.

global

=. 1 x
 DEF F1 ():
     Global x   # declared as global x x 
    x =. 3 
F1 ()    
Print (x)

nonlocal

For nested local function

def f1():
    x = 1
    def f2():
        nonlocal x  #取代x = 1 
        x = 3
    print()
f1()
​
#3

Variable Types

lt = [1,2,3]
​
def f1():
    lt.append(4)
    
f1()
print(lt)
​
#[1,2,3,4]

 

Scope relations: For variable type is not available, apply only immutable types.

Guess you like

Origin www.cnblogs.com/fxyadela/p/11329113.html