zoukankan      html  css  js  c++  java
  • [Python进阶]001.不定参数

    不定参数


    介绍

    • 不定参数用 *** 定义
    • 不定参数必须在其他所有参数之后
    • 例子:os.path.join 方法就可以写入不定数量的参数

    元组参数

    • 定义:*args*作为参数前缀
    • 会将传入的多个参数作为一个元组传入

    代码

    def fun(*args):
        for i in args:
            print i
    
    fun('a', 'b', 'c', 'd')
    
    def fun2(arg1, arg2, *args):        # 带2个固定参数
        print 'arg1:', arg1
        print 'arg2:', arg2
        print 'args:', args
    
    fun2('a', 'b', 'c', 'd', 'e')

    字典参数

    • 定义:**kwargs**作为参数前缀
    • 会将传入的多个参数作为一个字典传入

    代码

    def fun(**kwargs):
        for key in kwargs:
            print key, kwargs[key]
    
    fun(a='arg1', b='arg2')

    混合

    代码

    def fun(arg1, arg2, *args, **kwargs):
        print args
        print kwargs
    
    fun('a', 'b', 'c', 'd', key1='e', key2='f')

     

    本站文章为 宝宝巴士 SD.Team 原创,转载务必在明显处注明:(作者官方网站: 宝宝巴士 
    转载自【宝宝巴士SuperDo团队】 原文链接: http://www.cnblogs.com/superdo/p/4774247.html

     

  • 相关阅读:
    DateTimePicker常用格式类型
    自定义TreeNode
    memoのPython环境配置
    关于隐式积分方程的一些问题
    Position Based Dynamics【译】
    memoのMac折腾记录
    20210425 助教一周小结(第十二周)
    20210328 助教一周小结(第八周)
    20210307 助教一周小结(第五周)
    20210404 助教一周小结(第九周)
  • 原文地址:https://www.cnblogs.com/superdo/p/4774247.html
Copyright © 2011-2022 走看看