This article mainly introduces the usage of the apply function in Python. it can be used with examples to help readers better understand the apply function. For more information, see
I. Overview:
The python apply function has the following meanings:
The apply (func [, args [, kwargs]) function is used to call a function indirectly when a function parameter already exists in a tuple or dictionary. Args is a tuple that contains parameters that will be provided to the function by location. If args is omitted, no parameters will be passed. kwargs is a dictionary containing key parameters.
The return value of apply () is the return value of func (). The element parameters of apply () are ordered, and the element order must be consistent with the order of parameters in func () format.
II. example:
Here are a few examples to illustrate the usage of apply:
1. assume that the method without parameters is executed:
Def say (): print 'Say in' apply (say)
The output result is 'say in'
2. the Function only includes the parameters of the tuples:
Def say (a, B): print a, B apply (say, ("hello", "Michael python "))
The output result is hello, Michael python.
3. function parameters with Keywords:
Def say (a = 1, B = 2): print a, B def haha (** kw): # say (kw) apply (say, (), kw) print haha (a = 'a', B = 'B ')
The output result is: a, B