美文网首页
python带星号函数

python带星号函数

作者: 冬季恋歌1218 | 来源:发表于2018-04-23 11:12 被阅读47次

默认值函数参数。这种函数定义时,第一个有默认值的参数后的每一个参数都必须提供默认值。传参时,可以直接传参,也可以以“默认值参数名=value”的形式传参。

单星号函数参数。单星号函数参数接收的参数组成一个元组。

单星号参数,把接收的参数作为元组,如果直接传元组类型的值,则传递的元组值作为了星号参数的元组中的一个元素。若要把元组值就作为星号参数的参数值,在元组值前加上“*”即可,不过此时,就不能在加了“*”的元组后,追加任何值了。

双星号函数参数。双星号函数参数接收的参数组成一个字典。必须采用默认值传参的“args = value”的方式。同时,“=”前的字段成了字典的键,“=”后的字段成了字典的值。即,双星号参数接收的参数作为字典。若想把字典值就作为星号参数的参数值,那么该怎么办呢?同单星号参数,在字典值前加上“**”,同时其后不能添加任何值。

#!/usr/bin/python

def singalStar(common, *rest):

    print("Common args: ", common)

    print("Rest args: ", rest)

def doubleStar(common, **double):

    print("Common args: ", common)

    print("Double args: ", double)

def singalAndDoubleStar(common, *single, **double):

    print("Common args: ", common)

    print("Single args: ", single)

    print("Double args: ", double)

def defaultValueArgs(common, defaultStr = "default", defaultNum = 0):

    print("Common args", common)

    print("Default String", defaultStr)

    print("Default Number", defaultNum) 

if __name__ == "__main__":

    defaultValueArgs("Test")

    defaultValueArgs("Test", "defaultqqq", defaultNum = 1)

    singalStar("hello")

    singalStar("hello", "world", 000)

    singalStar("hello", ("world", 000))

    singalStar("hello", ("world", 000), {123: 123})

    singalStar("hello", *("world", 000))

#    singalStar("hello", *("world", 000), "123")    #error

    doubleStar("hello")

    doubleStar("hello", x = "Test", y = 24)

    doubleStar("hello", **{"name": "Test", "age": 24})

#    doubleStar("hello", {"name": "Test", "age": 24})    #error

    singalAndDoubleStar("hello")

    singalAndDoubleStar("hello", "world", 000)

    singalAndDoubleStar("hello", "world", 000, {"name": "Test", "age": 24})

    singalAndDoubleStar("hello", "world", 000, **{"name": "Test", "age": 24})

    singalAndDoubleStar("hello", ("world", 000), {"name": "Test", "age": 24})

#    singalAndDoubleStar("hello", *("world", 000), {"name": "Test", "age": 24})      #error

    singalAndDoubleStar("hello", *("world", 000), **{"name": "Test", "age": 24})

相关文章

  • python带星号函数

    默认值函数参数。这种函数定义时,第一个有默认值的参数后的每一个参数都必须提供默认值。传参时,可以直接传参,也可以以...

  • 11道面试中不常见却一定会问到Python题解析

    1、请用python编写函数find_string,从文本中搜索并打印内容,要求支持通配符星号和问号。 例子: 解...

  • python里星号

    python里星号有两种意思 1. 定义函数时一般情况下,函数的参数接受指定个数的参数,比如def func(a,...

  • 带星号了

    时间已经很晚,今天始终生活在疫情的阴影下,因为我们的行程卡带星号了。 福建的疫情还没结束,黑龙江的疫情又冒了头,一...

  • 带星号了

    文/明日之月 从昨晚杭州报出新确诊病例后,行程码上杭州就开始带上星号了,现在就尽量少走动,不去人多聚集性的地方。 ...

  • 18章 参数

    函数能够⽤⼀个星号""或两个星号"*"开头的特殊参数,来收集任意多的额外参数。 在函数调⽤和函数定义中,如果出现a...

  • python中星号变量的特殊用法

    引言 在Python中,星号除了用于乘法数值运算和幂运算外,还有一种特殊的用法"在变量前添加单个星号或两个星号",...

  • python学习:python的星号(*)和双星号(**)用法

    https://www.cnblogs.com/empty16/p/6229538.html

  • c++函数指针

    仅限快速使用。对于某个函数: 你可以: 这样 f 就跟add一模一样,add怎么用 f 就怎么用,不用管什么带星号...

  • ES6 async 函数

    是什么 async函数就是将 Generator 函数的星号(*)替换成async,将yield替换成await,...

网友评论

      本文标题:python带星号函数

      本文链接:https://www.haomeiwen.com/subject/yjjzkftx.html