Summary of basis functions

Summary of basis functions

First, the definition function

Only detect syntax, the code does not execute

Two, three way function definition

  1. There are function parameters
  2. No-argument function
  3. Empty function

Third, call the function

def f1():
    return 123

f = f1()
f1()*2
lis = [f1(),2]

Fourth, the return value of the function

Return return value, the function will return encounter terminates, return values ​​return any data type, but also can return multiple values

Function only in the call phase will receive the return value, default return None

Fifth, the parameters of the function

5.1 parameter

Receiving arguments, descriptive sense, there is no specific value

5.2 arguments

It has a specific value, to the reference mass parameter

  1. Location parameter: a left to right argument a receiving position
  2. Position argument: a pass from left to right a parameter
  3. Keyword argument: According to the parameter name mass participation
  4. Default parameter: parameter to a default value, if the argument does not pass parameter values, the default value; the other hand, using the value of the argument pass
  • Keyword argument must have a position behind the argument, a default parameter must parameter in the rear position
  • A parameter value can only receive one
def f1(x,y):
    pass

f1(1,x=2)  # 报错

Sixth, the variable-length parameters

  1. * Parameter: receiving extra argument position, so as to store tuple
  2. * Arguments: the broken into a tuple argument position, and then passed parameter (not recommended, only learn)
  3. ** parameter: a keyword receiving extra argument to the dictionary stored
  4. ** argument: the dictionary scattered called a keyword argument, then passed to the parameter (not recommended, only to understand)
  • keep in mind:
def f1(*args,**kwargs):
    pass

# f1可以接收所有的参数
f1(1,1,2,3,3,4,5,x=2,y=5,a=8)

Seven function object

def f1():
    pass
  1. Quotef=f1
  2. As a function return valuereturn f1
  3. As a function parameter passingf2(f1)
  4. Container elementlis=[f1]

Eight, nested functions

def f1():
    def f2():
        pass
    
f2()  # 报错

Nine, the name space and scope

  1. Built-in namespace: storage built-in names such aslen/eval/enumerate/bytes/max/min/sorted/map/filter....
  2. Global name space: in addition to built with local, other names are stored in the global name space within
  3. The local name space: the internal name of the function are local namespaces, different internal name of non-interference function
  • Find the order: Looking up from the beginning of the current, if the current is the local name space, find the following order: Local -> Global -> Built-in
  • Execution order: first built (Python interpreter startup time will generate) -> Global (when performed on the file will be generated) -> local (function call time will generate)

Scope: There may be the same variable names global namespace and local name space, but these two variables affect each other. Only for immutable data types, except for variable data type, try not to use this feature variable type, if you really use, you can define the different functions of different variables out.

Guess you like

Origin www.cnblogs.com/randysun/p/12240425.html
Recommended