Example of using the Apply function in Python tutorial _python

Source: Internet
Author: User
Tags in python

First, overview:

The specific meaning of the Python apply function is as follows:

Apply (func [, args [, Kwargs]]) functions are used to invoke functions indirectly when function arguments already exist in a tuple or dictionary . Args is a tuple that contains the arguments that are passed by location that will be supplied to the function. If args is omitted, no arguments are passed, Kwargs is a dictionary that contains the keyword parameters.

The return value of apply () is the return value of func (), the element parameter of the Apply () is ordered, and the order of the elements must be the same as the order of the Func () Form parameters

Second, the use of examples:

Here are a few examples to illustrate the use of apply in detail:

1, the assumption is that there are no parameters with the method:

Def say ():
 print ' say in '

apply (say)

The result of the output is ' say in '

2, the function takes only the parameters of the tuple:

Def say (A, b):
 print A, b
 
apply (say, ("Hello", "John Python")

The result of the output is Hello, John Python

3, function with keyword parameters:

def say (a=1,b=2):
 print a,b
 
def haha (**kw):
 #say (kw)
 apply (say, (), kw)
 
print haha (a= ' a ', b= ' B ')

The result of the output is: a,b

Related Article

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.