Arguments review

1. Why use functions?
      Reduce code redundancy
2. The function uses the first definition (as after the first corresponds to the variable definitions used)
Classification 3. Function:
      Built-in functions: python interpreter carrying, directly used on the list of
      custom functions: according to their function needs its own definition
       def func (): # function name printed memory address
       '' 'Notes' ''
       function body
4. the function definitions have done what things?
     Only detect syntax without executing code
is some function 5. function, as far as possible defined function name meaningful
three ways 6. Define the function:
  there are function parameters: accept the value is passed in the outside, a series of operations, Finally, it returns the results came out
  function without parameters: usually just do some operate only
  an empty function: do nothing is an empty function (even an empty function, he will have the following attributes)
7. the return value of a function: can the return value of any type
  when you need to return a value: specific conditions
  when there is no return value: specific conditions
distinguish 8.yield and return of
  yield were stopped at it, hang the
  yield can return multiple values; return returns a value
  yield saving function of the state
9. call function: the function name ()
10. the function parameters
  parameter: defining a function written in parentheses parameters
  During the definition phase parameter is not occupy memory space, when the memory space occupied before the call
  arguments: passed in brackets when calling the function value
the specific use of function parameter
 1. Location parameters:
    1. In accordance with the position of mass participation
      by defined position parameter values must pass
      arguments defined by location parameter according to a position one-pass
    2. the transmission number keywords: argument defined in the form of a key = value, and when the transfer position is not relationship
      DEF foo (X, Y):
        pass
      foo (1,2) # correspondence with the parameter
      foo (y = 2, x = 1) # keyword parameters, pass by name
      foo (1, y = 2 ) # mix position arguments and argument keywords
           # used when mixed with: Note: 1. can not be repeated a parameter assigned to the same
   front position 2. the argument must be in the keyword arguments
2. default parameters: in the definition phase It has to parameter assignment, meaning the call phase can not pass parameters to the default value
        if the parameter passed to the default value, and put a beginning set to replace.
    The default parameter defines the scene: Most are men, women rarely when you can set a default, of course, there are other scenarios
Note: 1. The default parameters must be in the rear position parameter
       2. The default value of the parameter only the function definition stage take effect once, after modifying the definition does not affect its value
       3. the default parameters do not inflicted variable type, like a list, that is, a pit
 3. dynamic parameters
    Parameters of variable length (length refers to the number of parameters): Parameter *, **
    1 * will pass extra value Z, and returned as a tuple of   
    2 ** put extra. values to the Z, and returns the form dictionary
    3. as long as the arguments encountered * (), is broken up
     as long as it can be used iterative *
   * equivalent for loop operation, the first encountered * break
   * args, ** kwargs (arbitrary value can pass)
of the variable-length parameters: angle calculated from the argument of the corresponding parameter in the form * and **
for according to a defined position extra argument that part, will be * the parameter processing, and stored in the form of a copy of the tuple args
for argument defined according to keywords that extra part, will be processed * the parameter and stored in the form of a copy of dictionary kwargs

 

  1 # 1.举例一、
  2 def foo(x,y,*args):
  3     print(x,y)
  4     print(args)#args相当于(3, 4, 5, 6, 7, 8, 9),那么*args相当于*(3, 4, 5, 6, 7, 8, 9)
  5     print(args[0])
  6     print(*args) # 加个*就是把上面的元组给打散了,就还是和原来传实参的时候的形式一样了
  7 
  8 foo(1,2,3,4,5,6,7,8,9)
  9 foo(1,2,3,*(4,5,6,7,8,9)) #也可以这样传 就相当于foo(1,2,3,4,5,6,7,8,9)
 10 
 11 # 2.举例二、
 12 def bar(x,y,z):
 13     print(x,y,z)
 14 bar(1,2,3)
 15 bar(*['b','a','c']) #bar('b','a','c')
 16 bar(*'hel') #bar('h','e','l')
 17 bar(*{'a':1,'b':2,'c':3}) #bar('b','a','c')
 18 # 强调:如果实参中出现了*和**,第一时间打散了去看
 19 
 20 # 3.举例三、
 21 def foo(x,y,**kwargs):
 22     print(x,y)
 23     print(kwargs) #{'c': 3, 'd': 4, 'f': 6} 吧多余的元素以字典的形式返回了
 24     print(*kwargs) #输出c d f ,就是把字典打散了
 25 #
 26 # foo(1,y=2,c=3,d=4,f=6)
 27 
 28 # 4.举例四、
 29 def index(x,y,z=1):
 30     print(x,y,z)
 31 def wrapper(*args,**kwargs):
 32     index(args,kwargs)
 33     index(*args, **kwargs) #就是打散了
 34 wrapper(1,2,3)
 35 wrapper(1,2)
 36 wrapper(x=1,y=2)
 37 wrapper(1,y=2)
 38 '''可变长度的参数:从实参的角度推算出形参对应的形式 * 和 **
 39 针对按照位置定义的实参冗余的部分,会被形参中的 * 保存成元组的形式赋值给args
 40 针对按照关键定义的实参冗余的部分,会被形参中的 ** 保存成字典的形式赋值给kwargs
 41 强调:
 42 如果实参中出现了 * 和 **,第一时间打散了去看。
 43 '''

 

强调:如果实参中出现了*和**,第一时间打散了去看
11.命名关键字参数(了解):在定义阶段,在*之后定义的形参称为命名关键字参数
    特点是:在调用阶段,该参数必须以关键字的形式被传值
def foo(x,y,*,a,b):
print(x)
print(y)
print(a)
print(b)
foo(1,2,b=20,a=10)
控制函数调用的时候必须以关键字传参的时候就用命名关键字传参

 

 

归类 : Python相关

Guess you like

Origin www.cnblogs.com/lz1996/p/11568257.html
Recommended