Write todayCodeThePython applyThe method is very easy to use, so I wroteArticleThis article will be introduced to you.
Python apply function:
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 ancestor parameter of apply () is ordered, and the element order must be consistent with the order of parameters in func () format.
The following is a detailed example:
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", "Old Wang Python "))
3. The function has a keyword parameter.
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
For some friends, it is a little difficult to understand the operations of 3rd functions with keywords. Others should be relatively simple. If you think the operations of 3rd functions are difficult,
You can write more code to practice.
Here is an example of the classic application of apply, which allows you to write less code and spend more time with friends.
The address is:
Http://bbs.cnpythoner.com/viewthread.php? Tid = 139 & extra =
For the original article, please indicate that it is reprinted from Lao Wang python. The address of this article is:
Author: Lao Wang @ Python tutorial
Old Wang Python provides Python-related Django tutorials and Python downloads!