当前位置:首页 > 编程技术 > 正文内容

Python 中的默认值是什么?

yc8887个月前 (02-23)编程技术311

Python 中的默认值是什么?

Python 语言具有表示函数参数的语法和默认值的不同方式。

默认值指示如果在函数调用期间未给出参数值,则函数参数将采用该值。默认值是使用表单关键字名称=值的赋值 (=) 运算符分配的。

# creating a function by giving default values
def tutorialspoint(website, author ='XYZ', language ='Python'):
   print(website, "website article is written by the author", author,"of language", language)

不带关键字参数的函数调用


# creating a function by giving default values def tutorialspoint(website, author ='XYZ', language ='Python'): print(website, "website article is written by the author", author,"of the language", language) # calling only 1 positional argument(website) # here it takes the given default values for author(XYZ) and language(Python) tutorialspoint('tutorialspoint') # calling 3 positional arguments (website, author, language) tutorialspoint('tutorialspoint', 'Alex', 'Java') # calling 2 positional arguments (website, author) # here it takes the given default value for the language(Python) tutorialspoint('tutorialspoint', 'Gayle') # here it takes the given default value for author(XYZ) tutorialspoint('tutorialspoint', 'C++')

输出

在执行时,上述程序将生成以下输出 –

tutorialspoint website article is written by the author XYZ of the language
Python
tutorialspoint website article is written by the author Alex of the language
Java
tutorialspoint website article is written by the author Gayle of the language
Python
tutorialspoint website article is written by the author C++ of language Python

解释

  • 在第一种情况下,第一次调用中只有一个必需的参数,其余参数设置为默认值。

  • 在第二个函数调用中,我们调用了一个具有 3 个位置参数(网站、作者、语言)的函数。作者标准参数的值从默认值更改为新的传递值。

使用关键字参数调用函数


# creating a function by giving default values def tutorialspoint(website, author ='XYZ', language ='Python'): print(website, "website article is written by the author", author,"of language", language) # calling only 1 positional argument(website) with keywords # here it takes the given default values for author(XYZ) and language(Python) tutorialspoint(website= 'tutorialspoint') # calling 2 positional arguments (website, language) with keywords tutorialspoint(website= 'tutorialspoint', language = 'Java') # calling 2 positional arguments (author, website) with keywords # the order of the keywords is not mandatory # here it takes the given default value for language(Python) tutorialspoint(author= 'Gayle', website= 'tutorialspoint')

输出

在执行时,上述程序将生成以下输出 –

tutorialspoint website article is written by the author XYZ of language Python
tutorialspoint website article is written by the author XYZ of language Java
tutorialspoint website article is written by the author Gayle of language Python

解释

  • 第一次调用中只需要一个关键字参数。

  • 在第二次调用中,一个参数是必需的,另一个是可选的(语言),其值从默认值更改为新的传递值。

  • 我们可以从第三次调用中看到,关键字参数的顺序不重要/不是强制性的。

无效的函数调用(引发错误)

现在我们介绍一些抛出错误的函数调用的无效情况。


# creating a function by giving default values def tutorialspoint(website, author ='XYZ', language ='Python'): print(website, "website article is written by the author", author,"of language", language) # No arguments are passed to the function, hence an error occurs tutorialspoint()

输出

在执行时,上述程序将生成以下输出 –

Traceback (most recent call last):
File "main.py", line 5, in <module>
tutorialspoint()
TypeError: tutorialspoint() missing 1 required positional argument: 'website'

没有参数传递给函数,因此会发生错误


# creating a function by giving default values def tutorialspoint(website, author ='XYZ', language ='Python'): print(website, "website article is written by the author", author,"of language", language) # There is a non-keyword argument for author(Alex) after # a keyword argument website(tutorialspoint). Hence an error occurs tutorialspoint(website= 'tutorialspoint', 'Alex')

输出

在执行时,上述程序将生成以下输出 –

File "main.py", line 6
    tutorialspoint(website= 'tutorialspoint', 'Alex')
                                              ^
SyntaxError: positional argument follows keyword argument

在关键字参数之后,有一个作者(Alex)(tutorialspoint)的非关键字参数。因此,会发生错误。


# creating a function by giving default values def tutorialspoint(website, author ='XYZ', language ='Python'): print(website, "website article is written by the author", author,"of language", language) # The keyword address is not defined in the function(unknown keyword argument) # hence it raises an error tutorialspoint(address ='Hyderabad')

输出

在执行时,上述程序将生成以下输出 –

Traceback (most recent call last):
  File "main.py", line 6, in <module>
tutorialspoint(address ='Hyderabad')
TypeError: tutorialspoint() got an unexpected keyword argument 'address'

由于函数(未知关键字参数)中未定义关键字地址,因此会引发错误。

使用可变对象作为默认参数

必须非常小心地进行。原因是当控件到达函数时,参数的默认值仅计算一次。

第一次,一个定义。之后,在后续函数调用中引用相同的值(或可变对象)。


# Creating a function by passing the list element, new list as arguments # the function returns a new list after appending elements to it def appendingElement(listElement, newList = []): newList.append(listElement) # returning a new list return newList # calling function by passing the list element as an argument # and printing the result new list print(appendingElement('hello')) print(appendingElement('tutorialspoint')) print(appendingElement('python'))

输出

['hello']
['hello', 'tutorialspoint']
['hello', 'tutorialspoint', 'python']

结论

我们在本文中了解了 Python 函数中的默认值。我们通过一些示例了解了默认参数和参数。


本站发布的内容若侵犯到您的权益,请邮件联系站长删除,我们将及时处理!


从您进入本站开始,已表示您已同意接受本站【免责声明】中的一切条款!


本站大部分下载资源收集于网络,不保证其完整性以及安全性,请下载后自行研究。


本站资源仅供学习和交流使用,版权归原作者所有,请勿商业运营、违法使用和传播!请在下载后24小时之内自觉删除。


若作商业用途,请购买正版,由于未及时购买和付费发生的侵权行为,使用者自行承担,概与本站无关。


本文链接:https://www.10zhan.com/biancheng/10592.html

标签: Python

“Python 中的默认值是什么?” 的相关文章

【说站】Thymeleaf报错Error resolving template “XXX”

【说站】Thymeleaf报错Error resolving template “XXX”

修改了一下开源项目的目录结构访问突然报错Error resolving template “XXX”可能原因有如下三种:第一种可能:原因:在使用springboot的过程中,如果使用thymeleaf...

【说站】Centos8.0如何配置静态IP详解及永久关闭防火墙

【说站】Centos8.0如何配置静态IP详解及永久关闭防火墙

这篇文章主要介绍了详解Centos8 配置静态IP的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来学习一下!1. 查看自己的网关地址点击虚...

【说站】电脑安装MySQL时出现starting the server失败原因及解决方案

【说站】电脑安装MySQL时出现starting the server失败原因及解决方案

今天在安装MySQL时出现starting the server失败,经过查询分析得出以下结论,记录一下操作步骤。原因分析:如果电脑是第一次安装MySQL,一般不会出现这样的报错。如下图所示。star...

【说站】C#在PDF中添加墨迹注释Ink Annotation的步骤详解

【说站】C#在PDF中添加墨迹注释Ink Annotation的步骤详解

PDF中的墨迹注释(Ink Annotation),表现为徒手涂鸦式的形状;该类型的注释,可任意指定形状顶点的位置及个数,通过指定的顶点,程序将连接各点绘制成平滑的曲线。下面,通过C#程序代码介绍如何...

【说站】使用systemctl配置dnspod-shell实现ddns

【说站】使用systemctl配置dnspod-shell实现ddns

这个是毛子路由器上用的脚本,由于碳云的nat服务器公网IP不断的变,因此只好通过ddns来稳定连接nat服务器了。顺便水一篇文章,大家新年快乐。使用前需要将域名添加到 DNSPod 中,并添加一条A记...

【说站】Python获取最新疫情数据实现动态地图实时展示各地情况

【说站】Python获取最新疫情数据实现动态地图实时展示各地情况

疫情降临转眼已经第三年了,时间过得真快,愿疫情早点结束,世界不再多灾多难。最近疫情稍微好转一些了,所以咱们获取一下最新的疫情数据,做个可视化地图看看。效果展示获取到的数据咱们保存到表格可视化地图颜色是...