Python基本语法_函数_参数的多类型传值

举报
云物互联 发表于 2021/08/06 01:20:42 2021/08/06
【摘要】 前言 上一篇主要介绍了Python函数的参数类型,本篇继续学习Python函数多类型传值。 目录 前言目录软件环境参数的多类型传值 向函数传递Tuple向函数传递List向函数传递Dictionary 软件环境 系统 UbuntuKylin 14.04软件 Python 2.7.4IPython 4.0.0 参数的多类型传值 一...

前言

上一篇主要介绍了Python函数的参数类型,本篇继续学习Python函数多类型传值。

目录

软件环境

  • 系统
    • UbuntuKylin 14.04
  • 软件
    • Python 2.7.4
    • IPython 4.0.0

参数的多类型传值

一般而言,形参和实参的数量要一致。但是当实参为序列数据类型时,我们可以将多个实参传递到形参中。
我们可以在实参列表中以 * 或者 ** 标识符来限制传入的实参必须为 Tuple/List 或 Dict 的类型对象。

向函数传递Tuple

其实不定长参数本质是将冗余的实参转换为Tuple数据类型后再传递给函数。下面的例子传递了一个Tuple类型实参给函数,和使用不定长参数效果是一样的。需要注意的是,不定长参数需要放到函数形参列表的后端,而Tuple类型形参可以随意放置。

In [33]: %pycat func1Test.py
#!/usr/bin/env python
#Filename:func1Test.py
import sys
def printYourEnter(input1,input2): print input1 print input2 for var in input2: print var

if __name__ == '__main__': enter1 = raw_input("Ple enter your living cities:") enter2 = raw_input("ple enter your name:") enter3 = raw_input("Ple enter your age:") tuple1 = (enter2,enter3) printYourEnter(enter1,tuple1) # 这里没有限制实参类型,可以传入任意类型对象

In [34]: run func1Test.py
Ple enter your living cities:BJ
ple enter your name:Jmilk
Ple enter your age:23
BJ
('Jmilk', '23')
Jmilk
23
  
 

注意:向函数传度Tuple还有一种形式:

In [91]: %pycat tupleTest.py
#!/usr/bin/env python
#Filename:tupleTest.py
def test(x,y): print x*y
if __name__ == '__main__': t = (2,3) test(*t) # 限制只能传入 Tuple 类型对象

In [92]: run tupleTest.py
6
  
 

在调用函数的时候,使用 * 标识符:标识将 t 元组中的元素以迭代的方式传递给形参。但是这种时候要求元组元素的个数与形参的个数要一致。

向函数传递List

向函数传递List类型对象,与传递Tuple类型对象类似。

In [61]: %pycat func1Test.py
#!/usr/bin/env python
#Filename:func1Test.py
def printYourEnter(input1,x,y): print input1 print x print y

if __name__ == '__main__': enter1 = raw_input("Ple enter your living cities:") enter2 = raw_input("ple enter your name:") enter3 = int(raw_input("Ple enter your age:")) li1 = [enter2,enter3] printYourEnter(enter1,*li1)

In [62]: run func1Test.py
Ple enter your living cities:BJ
ple enter your name:Jmilk
Ple enter your age:23
BJ
Jmilk
23
  
 

向函数传递Dictionary

想函数传递Dic数据类型需要注意下面几点:
1.形参名与dic中的key相同,形参会自左往右的匹配第一个与自己同名的key,并将此key映射的value传递到形参。
2.若有形参在dic中并没有找到匹配的key,则会报错。
3.若dic中有多个同名的key,则自右往左的优先级来匹配。
4.形参与dic的key:value顺序无关。
注意:若dic中有key不能与形参匹配时,需要人为的指定dic中key给形参,并传递其value给形参,否则会报错。本质是将dic中的value值先取出来再传递到函数形参

functionName(dic[x][,...])
  
 

Example:

In [45]: %pycat func1Test.py
#!/usr/bin/env python
#Filename:func1Test.py
def printYourEnter(input1,cities,name,age): print input1 print cities print name print age

if __name__ == '__main__': enter1 = raw_input("Ple enter your living cities:") enter2 = raw_input("ple enter your name:") enter3 = int(raw_input("Ple enter your age:")) dic = {'cities':enter1,'name':enter2,'age':enter3} printYourEnter('a',**dic)

In [46]: run func1Test.py
Ple enter your living cities:BJ
ple enter your name:Jmilk
Ple enter your age:23
a
BJ
Jmilk
23
  
 

当限制向函数传递一个dic数据类型时,在调用函数时要使用 ** 来标识是一个映射,即传递一个字典类型的对象。而且在函数定义时,函数的形参必须与字典的Key一致,才可以实现将字典的value传递给函数中对应的形参。
也可以写成下面这种形式:

In [49]: %pycat func1Test.py
#!/usr/bin/env python
#Filename:func1Test.py
def printYourEnter(input1,dic): print input1 print dic['cities'] print dic['name'] print dic['age']

if __name__ == '__main__': enter1 = raw_input("Ple enter your living cities:") enter2 = raw_input("ple enter your name:") enter3 = int(raw_input("Ple enter your age:")) dic = {'cities':enter1,'name':enter2,'age':enter3} printYourEnter('a',dic)

In [50]: run func1Test.py
Ple enter your living cities:BJ
ple enter your name:Jmilk
Ple enter your age:23
a
BJ
Jmilk
23
  
 

Jmilk

文章来源: is-cloud.blog.csdn.net,作者:范桂飓,版权归原作者所有,如需转载,请联系作者。

原文链接:is-cloud.blog.csdn.net/article/details/49358153

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。