Example of how to use the apply function in Python

Source: Internet
Author: User
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

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.