Home > Backend Development > Python Tutorial > Python中apply函数的用法实例教程

Python中apply函数的用法实例教程

WBOY
Release: 2016-06-16 08:43:02
Original
2674 people have browsed it

一、概述:

python apply函数的具体含义如下:
 
apply(func [, args [, kwargs ]]) 函数用于当函数参数已经存在于一个元组或字典中时,间接地调用函数。args是一个包含将要提供给函数的按位置传递的参数的元组。如果省略了args,任何参数都不会被传递,kwargs是一个包含关键字参数的字典。
 
apply()的返回值就是func()的返回值,apply()的元素参数是有序的,元素的顺序必须和func()形式参数的顺序一致

二、使用示例:

下面给几个例子来详细的说明一下apply的用法:

1、假设是执行没有带参数的方法:

def say():
 print 'say in'

apply(say)

Copy after login

输出的结果是'say in'

2、函数只带元组的参数:

def say(a, b):
 print a, b
 
apply(say,("hello", "张三python"))
Copy after login

输出的结果是hello,张三python

3、函数带关键字参数:

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

输出的结果是:a,b

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template