Python apply knowledge

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.