Python 带星号(* 或 **)的函数参数详解

 更新时间:2021年02月23日 10:02:33   作者:Jianzhi.Z  
这篇文章主要介绍了Python 带星号(* 或 **)的函数参数详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

1. 带默认值的参数

在了解带星号(*)的参数之前,先看下带有默认值的参数,函数定义如下:

>> def defaultValueArgs(common, defaultStr = "default", defaultNum = 0):
    print("Common args", common)
    print("Default String", defaultStr)
    print("Default Number", defaultNum) 
 

(1)带默认值的参数(defaultStr、defaultNum)不传参时的调用:

>> defaultValueArgs("Test")
 
Common args Test
Default String default
Default Number 0
 

(2)带默认值的参数(defaultStr、defaultNum),调用的时候可以直接传参(如下例中的defaultStr),也可以写成“argsName = value”的形式(如下例中的defaultNum):

>> defaultValueArgs("Test", "Str", defaultNum = 1)
 
Common args Test
Default String Str
Default Number 1
 
>> defaultValueArgs("Test", defaultNum = 1)
 
Common args Test
Default String default
Default Number 1

注意:在函数定义时,第一个带有默认值的参数之后的所有参数都必须有默认值,否则,运行时报错。

>> def defaultValueArgs(common, defaultStr = "default", defaultNum):
    print("Common args", common)
    print("Default String", defaultStr)
    print("Default Number", defaultNum)
    
SyntaxError: non-default argument follows default argument
 

2.带一个星号(*)的函数参数

带一个参数的函数定义如下:

>> def singalStar(common, *rest):
  print("Common args: ", common)
    print("Rest args: ", rest)

(1)带星号(*)的参数不传参:

>> singalStar("hello")
 
Common args: hello
Rest args: ()

带星号(*)的参数不传参时默认是一个空的元组。

(2)带星号(*)的参数传入多个值时(个数大于或等于函数定义时的参数个数):

>> singalStar("hello", "world", 000)
 
Common args: hello
Rest args: ('world', 0)

不难看出,第二种方式中,星号参数把接收的多个参数合并为一个元组。

(3)当我们直接传元组类型的值给星号参数时:

>> singalStar("hello", ("world", 000))
 
Common args: hello
Rest args: (('world', 0),)

此时,传递的元组值作为了星号参数的元组中的一个元素。

(4)如果我们想把元组作为星号参数的参数值,在元组值前加上" * " 即可。

>> singalStar("hello", *("world", 000))
Common args: hello
Rest args: ('world', 0)

>> singalStar("hello", *("world", 000), "123")
Common args: hello
Rest args: ('world', 0, '123')

3.带两个星号(**)的函数参数

带两个星号(**)的函数定义如下:

>> def doubleStar(common, **double):
    print("Common args: ", common)
    print("Double args: ", double)

(1)双星号(**)参数不传值:

>> doubleStar("hello")
 
Common args: hello
Double args: {}

带双星号(**)的参数不传值时默认是一个空的字典。

(2)双星号(**)参数传入多个参数时(个数大于或等于函数定义时的参数个数):

>> doubleStar("hello", "Test", 24)
TypeError: doubleStar() takes 1 positional argument but 3 were given

>> doubleStar("hello", x = "Test", y = 24)
Common args: hello
Double args: {'x': 'Test', 'y': 24}

可以看到,双星号参数把接收的多个参数合并为一个字典,但与单星号不同的是,此时必须采用默认值传参的 “ args = value ” 的方式,“ = ” 前的字段成了字典的键,“ = ” 后的字段成了字典的值。

(3)如果想把字典作为星号参数的参数值,那么该怎么办呢?与单星号参数类似,在字典值前加上 “ ** ”,同时其后不能添加任何值。

>> doubleStar("hello", {"name": "Test", "age": 24})
TypeError: doubleStar() takes 1 positional argument but 2 were given

>> doubleStar("hello", **{"name": "Test", "age": 24}, {"name": "Test2", "age": 24})
SyntaxError: positional argument follows keyword argument unpacking

>> doubleStar("hello", **{"name": "Test", "age": 24}, **{"name": "Test2", "age": 24})
TypeError: doubleStar() got multiple values for keyword argument 'name'

>> doubleStar("hello", **{"name": "Test", "age": 24})
Common args: hello
Double args: {'name': 'Test', 'age': 24}

4、在有些情况下,单星号函数参数和双星号函数参数是一起使用的:

def singalAndDoubleStar(common, *single, **double):
  print("Common args: ", common)
  print("Single args: ", single)
  print("Double args: ", double)

singalAndDoubleStar("hello")
# Common args: hello
# Single args: ()
# Double args: {}
singalAndDoubleStar("hello", "world", 000)
# Common args: hello
# Single args: ('world', 0)
# Double args: {}
singalAndDoubleStar("hello", "world", 000, {"name": "Test", "age": 24})
# Common args: hello
# Single args: ('world', 0, {'name': 'Test', 'age': 24})
# Double args: {}
singalAndDoubleStar("hello", "world", 000, **{"name": "Test", "age": 24})
# Common args: hello
# Single args: ('world', 0)
# Double args: {'name': 'Test', 'age': 24}
singalAndDoubleStar("hello", ("world", 000), {"name": "Test", "age": 24})
# Common args: hello
# Single args: (('world', 0), {'name': 'Test', 'age': 24})
# Double args: {}
singalAndDoubleStar("hello", *("world", 000), {"name": "Test", "age": 24}) 
# Common args: hello
# Single args: ('world', 0, {'name': 'Test', 'age': 24})
# Double args: {}
singalAndDoubleStar("hello", *("world", 000), **{"name": "Test", "age": 24})
# Common args: hello
# Single args: ('world', 0)
# Double args: {'name': 'Test', 'age': 24}

到此这篇关于Python 带星号(* 或 **)的函数参数详解的文章就介绍到这了,更多相关Python 带星号参数内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • python实现汉诺塔方法汇总

    python实现汉诺塔方法汇总

    本文给大家汇总了几种使用Python结合递归算法实现汉诺塔的方法,非常的简单实用,对大家学习Python很有帮助,希望大家能够喜欢
    2016-07-07
  • 创建Shapefile文件并写入数据的例子

    创建Shapefile文件并写入数据的例子

    今天小编就为大家分享一篇创建Shapefile文件并写入数据的例子,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-11-11
  • 如何在VSCode上轻松舒适的配置Python的方法步骤

    如何在VSCode上轻松舒适的配置Python的方法步骤

    这篇文章主要介绍了如何在VSCode上轻松舒适的配置Python的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-10-10
  • Django项目创建到启动详解(最全最详细)

    Django项目创建到启动详解(最全最详细)

    这篇文章主要给大家介绍了关于Django项目创建到启动的步骤,本文介绍的方法算是最全最详细的一个项目,需要的朋友可以参考下
    2019-09-09
  • Python常见沙箱技术与沙箱逃逸避免方法详解

    Python常见沙箱技术与沙箱逃逸避免方法详解

    Python沙箱可以帮助你在安全的环境中运行不受信任的代码,本文将探讨 Python 沙箱的概念、常见的沙箱技术以及如何避免沙箱逃逸,感兴趣的可以了解下
    2024-01-01
  • python3的pip路径在哪

    python3的pip路径在哪

    在本篇文章里小编给大家分享的是关于python3中pip路径位置的相关文章,有兴趣的朋友们学习下吧。
    2020-06-06
  • python 循环while和for in简单实例

    python 循环while和for in简单实例

    下面小编就为大家带来一篇python 循环while和for in简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-08-08
  • Python爬虫入门案例之爬取二手房源数据

    Python爬虫入门案例之爬取二手房源数据

    读万卷书不如行万里路,学的扎不扎实要通过实战才能看出来,今天小编给大家带来一份python爬取二手房源信息的案例,可以用来直观的了解房价行情,大家可以在过程中查缺补漏,看看自己掌握程度怎么样
    2021-10-10
  • Python与数据库的交互问题小结

    Python与数据库的交互问题小结

    这篇文章主要介绍了Python与数据库的交互,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-12-12
  • 详解python3中的真值测试

    详解python3中的真值测试

    这篇文章主要介绍了详解python3中的真值测试,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-08-08

最新评论