Function parameter function object nested functions

Is divided into two function parameter #: 
# # parameter definition phase, the parameters specified in the brackets corresponds to the variable name #
# # argument in the call incoming phase value in parentheses corresponds to the value #
# def foo (x, y): # # parameter parameter does not take up memory space in the definition phase, the variable name is not assigned, does not occupy memory space
# Print (X, Y)
# foo (1,2) argument #
# call stage, the argument the value will be bound to the parameter, unbind after the call

# in python, classification parameters
# location parameters: according to the position from left to right, followed by the definition of parameters
# location parameter: must be transferred value, and more not one less row
# positions arguments: the one-pass parameter values
# DEF foo (X, Y):
# Print (X, Y)
# foo (1,2)

# key parameters: calling the function stage , in the form of arguments to call the key = value
# characteristics: by name to pass parameter values without depending on the position of
# DEF foo (X, Y):
# Print (X, Y)
# foo (Y =. 1 , X = 2)
# Note: keyword parameters can be used and an argument position, but the position of the keyword parameter argument must be placed behind the
# DEF foo (X, Y, Z):
# Print (X, Y, Z )
# foo (. 3, Y =. 1, Z = 2)

# # Identify parameters: the function definition stage, it has to parameter assignment, the call can not pass the stage value
DEF foo # (X, Y =. 1):
# Print (X, Y)
# foo (1,2). 1 # 2
# foo (100). 1 # 100

# parameters are typically used to position changes frequently, and the default parameters commonly used in most cases invariant parameter
# def students (name, age, gender = 'male'): # default parameters must be placed behind the position of the parameter
# Print (name, Age, Gender)
# Students. ( ' OBOS ', 18 is)
# Students. (' Love ', 18 is,' FEMALE ')
' ''
OBOS 18 is MALE
Love 18 is FEMALE
'' '

# default parameter values, is assigned only once in the definition, then unaffected
# res . 1 =
# DEF foo (X, Y = RES):
# Print (X, Y)
#
# = 10 RES
# foo (100). 1 # 100

# is usually the default parameter values should be immutable type

# variable-length parameters: in the function call, the number is not fixed argument value
# argument has the form, location and keyword arguments argument
solution # formal parameters: * and **
# DEF foo (X, Y, Z *) : # DEF foo (the X-, the y-, * args)
# Print (the X-, the y-)
Print # (Z)

# foo (1,2,3,4,5,6,7)
'' '
. 1 2
(. 3,. 4,. 5,. 6,. 7) # * overflow portion is accepted, the tuple form, assigned to the # * args Z
'' '
# foo (1,2, * [3,4,5,6,7]) * # encountered, we should think is the location parameter, direct their colors foo (1 , 2,3,4,5,6,7) # * args
'' '
. 1 2
(. 3,. 4,. 5,. 6,. 7)
' ''
# foo (. 1, * [2,3,4,5, 6, 7 are]) # foo (1,2,3,4,5,6,7) # * args
'' '
. 1 2
(. 3,. 4,. 5,. 6,. 7)
' ''
# DEF foo (X, Y):
# Print (X, Y)
#
# foo (* (1,2)). 1 # 2 # * args

# ** kwargs
# DEF foo (X, Y, kwargs **):
# Print (X, Y )
# Print (kwargs)

# foo (Y =. 1, X = 2, A =. 3, B =. 4, C =. 5)
'' '
2. 1
{' A ':. 3,' B ':. 4,'c': 5}
'''
Foo # (. 1 = Y, X = 2, ** { 'A':. 3, 'B':. 4, 'C':. 5})
'' '
2. 1
{' A ':. 3,' B ':. 4 , 'C':}. 5
'' '
# DEF foo (name, Age):
# Print (name, Age)
#
# foo (** {' name ':' OBOS ',' Age ': 18 is}) # OBOS 18 is

# warpper DEF (* args, ** kwargs): # key parameters must be placed behind the position of the parameter
# Print (args, kwargs)
#
# warpper (l, 2,3,. 1 = a, Y = 2, C =. 3) # { 'A':. 1, 'Y': 2, 'C':. 3}

# DEF bar (X, Y, Z):
# Print (X, Y, Z)
# DEF warpper (* args, kwargs **): args = # (. 1, 2,. 3) kwargs = { 'A':. 1, 'Y': 2, 'C':}. 3
# bar (* args, ** kwargs) # bar (* (1, 2, 3), ** { 'a': 1, 'y': 2, 'c': 3}) # bar (1,2,3, a = 1, y = 2,c=3)
#
# wrapper(1,2,3,a=1,y=2,c=3) 报错

# def bar(x,y,z):
# print(x,y,z)
DEF warpper # (* args, ** kwargs): args = # (. 1, 2,. 3)
# bar (* args, ** kwargs) # bar (* (. 1, 2,. 3))
#
# warpper (. 1, 3) # 123

# name keyword parameter refers to a parameter defined in the *, the value of this parameter must be delivered (unless it has a default value), and must be in the form of a key = value
# def foo ( X, Y, *, m, n-):
# Print (X, Y)
# Print (m, n-)

# foo (1,2,3,4) given #
# foo (1,2, m = 3 , n . 4 =)
'' '
. 1 2
. 3. 4
' ''
# DEF foo (X, Y, * args, m, n-):
# Print (X, Y)
# Print (m, n-)
#
# foo (1,2 , m =. 3, n-=. 4)
'' '
. 1 2
. 3. 4
' ''
# DEF foo (X, Y, * args, m, n-):
# Print (X, Y)
# Print (args)
# Print ( m, n-)
#
# foo (l, 2,3, m =. 3, n-=. 4)
'''
. 1 2
(. 3,)
. 3. 4
'' '
# DEF foo (X, Y, * args, m = 100, n-):
# Print (X, Y)
# Print (args)
# Print (m, n-)
#
# foo (l, 2,3, n-=. 4)
'' '
. 1 2
(. 3,)
100. 4
' ''

# functions are first-class objects: refers to a data transfer function can be used as
# may be referenced
# def foo (X, Y):
# Print (X, Y)
# foo = F
# F (1,2). 1 # 2
# can be used as the argument to the function
# DEF foo ():
# Print ( 'from foo')
bar # def (FUNC):
# Print (FUNC) to get the memory address # # <function foo AT 0x0000020748B64B88>
# FUNC () # bracketed calls from foo #
# bar (foo)
return value can be used as a function of the #
# def foo ():
# Print ( 'from foo')
# def bar():
Return foo #
# F = bar ()
# F () from foo #
# can be used as container type element
# DEF foo ():
# Print ( 'from foo')
# DEF bar ():
# return foo
# L = [foo, bar]
# Print (L) # [<AT 0x0000022FCE0B4B88 function foo>, <bar AT 0x0000022FCE0B4C18 function>]
# L [0] () from foo #

# recursive function
nested function calls #
# def my_max ( X, Y):
# IF X> = Y:
# return X
# the else:
# return Y

# DEF my_max (A, B, C, D):
# RES1 = my_max (A, B)
# RES2 = my_max (RES1, C)
# = RES3 my_max (RES2, D)
# return RES3
# nested definitions function: a function inside another function defines a
function #define internal use only

Guess you like

Origin www.cnblogs.com/0B0S/p/11959896.html