Function basis (b)

Vararg

Vararg: refers to the function call, the number of parameters passed may not be fixed

The function is called, the value of no more than two pass mode, one is the position of the argument, and the other argument is a keyword, and therefore must have formal parameters there are two solutions, respectively, in order to accept the overflow location argument (*) with the keyword arguments (**)

keep in mind

A variable length parameter of *

* Parameter will overflow in the position-all argument, and then stored in the form of tuples, the tuple is then assigned to the parameters *. Note that: * the name of the parameter convention for the args.

def func(name, pwd, *args): # *后可以跟其他参数,例如abc,但尽量不要使用,应该使用args,args是约定俗成的
    print('name:', name, 'pwd:', pwd) 
    print(args) # args会接收多余的位置实参,以元组形式存储
    return 1

res = func('xiaowu', 123456, 18, 170, 140)
print(res)

# name: xiaowu pwd: 123456
# (18, 170, 140)
# 1

Second, the variable length parameter **

** parameter of the spill-over-all key argument, and then store the dictionary form, then the parameters assigned to the dictionary **. Note that: the parameter name after the convention is kwargs **

def func(name, pwd, **kwargs): # **后可以跟其他参数,但尽量不要使用,应该使用kwargs,kwargs是约定俗成的
    print('name:', name, 'pwd:', pwd)
    print(kwargs) # kwargs会接收多余的关键字实参,以字典形式存储
    return 1

res = func('xiaowu', 123456, age = 18, height = 170, weight = 140)
print(res)

# name: xiwowu pwd: 123456
# {'age': 18, 'height': 170, 'weight': 140}
# 1

To understanding

A variable length argument of *

*, * The parameter value of the argument will after * stored in the form of tuples, broken into argument position, and then passed parameter

def func(name, pwd, x, y, z): # 我们用具体的参数去接收可变长实参,会返回具体的值
    print('name:', name, 'pwd:', pwd)
    print(x, y, z)
    return 1

tup = (18, 170, 140)
res = func('xiaowu', 123456, *tup)
print(res)

# name: xiaowu pwd: 123456
# 18 170 140
# 1
def func(name, pwd, *args): 
    print('name:', name, 'pwd:', pwd)
    print(args)
    return 1

tup = (18, 170, 140)
res = func('xiaowu', 123456, *tup)
print(res)

# name: xiaowu pwd: 123456
# (18, 170, 140)
# 1

Second, the variable-length argument **

The argument ** ** ** would parameter value stored in a dictionary, broken into Keyword argument, and then passed parameter

def func(name, pwd, **kwargs): # 我们用可变长形参去接收可变长实参,会以字典的形式返回
    print('name:', name, 'pwd:', pwd)
    print(kwargs)
    return 1

dic = {'age': 18, 'height': 170}
res = func('xiaowu', 123456, **dic)
print(res)

# name: xiaowu pwd: 123456
# {'age': 18, 'height': 170}
# 1
def func(name, pwd, age, height): # 我们用具体的参数去接收可变长实参,会返回具体的值
    print('name:', name, 'pwd:', pwd)
    print('age:', age, 'height:', htight)
    return 1

dic = {'age': 18, 'height': 170}
res = func('xiaowu', 123456, **dic)
print(res)

# name: xiaowu pwd: 123456
# age: 18 height: 170
# 1

Variable-length parameters used in conjunction * and **

We can use in a function * and **

def func(name, pwd, *args, **kwargs):
    print('name:', name, 'pwd:', pwd)
    print(args)
    print(kwargs)
    return 1

res = func('xiaowu', 123456, 1, 2, age=18, height=170)
print(res)

# name: xiaowu pwd: 123456
# (1, 2)
# {'age': 18, 'height': 170}
# 1

Function object

Functions are first-class objects, i.e. function may be used as the data processing.

Function name identical to a variable name, a variable name conventional method i.e., the function name also has been cited, as a container element, as function parameters, as the function return value.

Four functions function object

  1. Quote

    def func():
        print('from func')
    
    f = func
    print(f)
    
    # <function func at 0x000001B9B5742CA8>
  2. As an argument to a function

    def func():
        print('from func')
    
    def foo(m):
        m()
    
    foo(func)
    
    # from func
  3. It can be used as the return value of the function

    def func():
        print('from func')
    
    def foo(x):
        return x
    
    res = foo(func)
    print(res)
    res()
    
    # <function func at 0x0000018D5A072EE8>
    # from func
  4. Types of elements can be used as a container

    def func():
        print('from func')
    
    function_list = [func]
    function_list[0]()
    
    # <function func at 0x0000027B7C6C2CA8>
    # from func

Nested functions

Nested functions, is then defined function within the function. But the function of the internal-defined function, the function can not be used inside a function defined in an external function.

def f1():
    def f2():
        print('from f2')
    f2()

f2()

# NameError: name 'f2' is not defined
def f1():
    def f2():
        print('from f2')
    f2()

f1()

# from f2

Namespace and scope

First, the name space

Namespace: create a variable we said before, it will open up a space to store variable in memory. We have not said storage variable name, in fact, there is room for a binding relationship between a variable name and memory storage in memory, this space is called namespace. Name space is divided into built-in namespace, global namespace, local name space.

1. built-in namespace

Built-in namespace: python interpreter is unique, kept the python interpreter that comes with the name, such asint、len

Built-in namespace is automatically opened store's name comes python interpreter python interpreter when activated, destroyed after the python interpreter closed

2. The global name space

Global namespace: namespace in addition to built and local name space, others are stored in the global namespace, such as the following codex、func、l

Global name space will become effective when performed on the file, the file expire after execution

x = 1

def func():
    pass

l = [1, 2]

3. The local name space

The local name space: the name of the function body for storing generated during a function call, as the following codef2

The local name space in the file will be executed when the function is invoked during the entry into force, after the failure of the function execution

def f1():
    def f2():
        print('from f2')
    f2()

f1()

4. The order of execution

The execution order of three kinds of namespaces are: built -> Global -> Local

5. Find the order

A namespace is used to store a binding relationship between variable names and values, so look for the variable name must be found in the space of three names.

Find order: starting from your current location -> Local -> Global -> Built-in, irreversible looking for.

Second, the scope

Scope is the area of ​​action, an effect range. Scope into global scope and local scope.

1. The global scope

Global Scope: Global effective, all survived, and includes built-in namespace global name space, variable global scope can only be used in the big picture.

x = 1

def f1():
    x = 3

print(x)

# 1

2. Local Scope

The local scope: partially valid, temporary storage, only contains the local name space variables local scope can only be used in locally.

def f1():
    def f2():
        def f3():
            print(x)
        x = 2
        f3()
    f2()

f1()

# 2

3. Notes

Scope function relationships in the definition phase has been fixed is dead, there is no relationship with the caller.

4. Supplement

4.1 global keyword

global keyword can modify variables in global scope.

x = 1

def f1():
    global x
    x = 3

f1()
print(x)

# 3nonlocal关键字

4.2 nonlocal keyword

nonlocal keyword can modify variables in the local scope.

def f1():
    x = 1
    def f2():
        nonlocal x
        x = 3
    f2()
    print(x)

f1()

# 3

4.3 Note

  1. In the local want to modify the global variable type, it does not require any statement can be modified directly.

  2. In the immutable type you want to modify the local global, we need global statement, declared as global variables can be modified directly.

That scope relationship only immutable data types, variable data type is not applicable

Guess you like

Origin www.cnblogs.com/yunluo/p/11334386.html