zoukankan      html  css  js  c++  java
  • Python基本语法_函数_参数的多类型传值

    前言

    上一篇主要介绍了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

    相关阅读:

  • 相关阅读:
    一个例子学懂搜索引擎(lucene)
    在vs2008中设置jquery智能提示
    40个有用的jQuery技术和教程
    构建高性能ASP.NET站点之三 细节决定成败
    了解搜索引擎技术
    使用微软分布式缓存服务Velocity Part 2
    【转】构建高性能ASP.NET站点 开篇
    构建高性能ASP.NET站点之二 优化HTTP请求(前端)
    笔记:深入理解JVM 第4章 JVM性能监控与故障处理工具
    javascript 继承
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13311062.html
Copyright © 2011-2022 走看看