One hundred II

Function basis

A variable length function

The variable length parameters: the number of parameters in the function call, the incoming is not fixed

The function is called, the value passed in two ways, one is the position of the argument, and the other argument is a keyword, and therefore accept the parameter values ​​need to pass two ways, receive overflow pass values ​​two ways position arguments (*) and keyword arguments (**).

The variable length parameter 1.1 (*)

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

def sum_self(*args):
    res = 0
    for num in args:
        res += num
    return res
res = sum_self(1, 2, 3, 4)
print(res)

10

The variable length argument 1.2 (*)

The argument , will cycle parameter value * removed, broken into position argument. After arguments whenever encountered with *, it is the position argument should be broken into a position immediately see argument.

def func(x, y, z, *args):
    print(x, y, z, args)
func(1, *(1, 2), 3, 4)

1 1 2 (3, 4)

The variable length parameter 1.3 (**)

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

def func(**kwargw):
    print(kwargw)
func(a=5)

{'a': 5}

The variable length argument 1.4 (**)

The argument , a value of the cycle parameters will ** removed, broken into Keyword argument. After whenever encountered, it is the key arguments in arguments with ** should immediately broken into keyword arguments to see.

def func(x, y, z, **kwargs):
    print(x, y, z, kwargs)
func(1, 3, 4, **{'a': 1, 'b': 2})

1 3 4 {'a': 1, 'b': 2

1.5 named parameter keywords

Name Keyword parameter: function definition stage, parameters are named after the * key parameter.

Features: In the pass value must be passed in accordance with the value of key = value manner and key must be named keyword arguments specified parameter name.

Second, the function object

Functions are first-class objects, i.e. function may be used as the data processing. Function object has four functions:

  1. Quote

    def func():
        print('from func')
    
    x = 'hello world'
    y = x
    
    f = func
    print(f)
    
    <function func at 0x00000243CD0C1EA0>
  2. As an argument to a function

  3. It can be used as the return value of the function

  4. It can be used as container type elements

Third, nested functions

Internal function defined functions can not be used inside a function defined in an external function.

Now there is a demand, by giving a pass parameters to the function of a determined area of ​​a circle or circumference of a circle. That is thrown into the pile of tools within the toolbox, and then want to get a tool, available directly from the toolbox on the line.

from math import pi
def circle(radius, action='area'):
    def area():
        return pi * (radius**2)
    def perimeter():
        return 2*pi*radius
    if action == 'area':
        return area()
    else:
        return perimeter()
print(f"circle(10): {circle(10)}")
print(f"circle(10,action='perimeter'):{circle(10,action='perimeter')}")

circle(10): 314.1592653589793
circle(10,action='perimeter'): 62.83185307179586
def max2(x, y):
    if x > y:
        return x
    else:
        return y
def max4(a, b, c, d):
    res1 = max2(a, b)
    res2 = max2(res1, c)
    res3 = max2(res2, d)
    return res3
print(max4(1, 2, 3, 4))

4

Fourth, namespace and scope

4.1 namespace

Namespace (name spaces): there is room for a binding relationship between a variable name and variable memory storage in memory, and this space is called a namespace.

4.1.1 built-in namespace

Built-in namespace: Store Pyhton interpreter that comes with the name, such as int、float、len.

Life cycle: when the interpreter started to take effect, fail when the interpreter is closed.

4.1.2 The global name space

Global name space: In addition to internal and local names, the rest are stored in the global name space.

Life cycle: when the file is executed into effect, expire after file execution.

4.1.3 local name space

The local name space: the name of the function body used to store generated during the function call.

Life Cycle: take effect during the function call when the file is executed, expire after the function execution.

4.1.4 load order

Because .py file is opened by the Python interpreter, so it must be built after the name space in Python interpreter has finished loading, began to open the file, this time will produce a global name space, but there is a certain function in the file when it is called, it will begin to produce local name space, the name space for the loading order: built-in - "global -" local.

4.1.5 Search sequence

As the name space is used to store a binding relationship between the variable name and value, so whenever you want to find the name, it must be found in one of the three, look for the following order: start looking from the current location, if the current location the location is the local name space, look for the following order: local - "global -" built.

4.2 Scope

I.e., regional scope of action.

4.2.1 global scope

Global Scope: Global effective, global survival, and includes built-in namespace global name space.

4.2.2 local scope

The local scope: local small, temporary storage, containing only the local name space.

Guess you like

Origin www.cnblogs.com/tangceng/p/11334530.html