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 函数中的默认值。我们通过一些示例了解了默认参数和参数。
相关文章
- Python tips: 什么是*args和**kwargs?
- python: easyocr的安装和使用(easyocr 1.6.2 / Python 3.7.15 )
- python:pip升级pip本身和setuptools(Python 3.7.15)
- Python图形用户界面
- 【Python五篇慢慢弹】快速上手学python
- 华为OD机试 - 最大时间(Java & JS & Python)
- Python: 爬虫入门-python爬虫入门教程(非常详细)
- 学习python 115小时后,告诉想学爬虫的你,别怕,爬虫,没那么难抓!
- Python学到什么程度可以面试工作?
- Python语言学习:解决python版本升级问题集合(python2系列→Python3系列)导致错误的总结集合
- Python:利用python语言绘制多个子图经典案例、代码实现之详细攻略
- Python语言学习:基于python五种方法实现使用某函数名【func_01】的字符串格式('func_01')来调用该函数【func_01】执行功能
- Python编程专属骚技巧1
- 已解决2.Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and wi
- 已解决Python爬虫报错<response [406]>
- 专业还没选,有必要报班自学python吗?
- 〖Python 数据库开发实战 - Redis篇⑫〗- Redis的事务特性
- 从零开始学python | 什么是Python JSON?
- 10个Python实战编程项目,有趣又好玩
- 写网络爬虫天然就是择Python而用 python 网络爬虫3
- 数据分析师要学什么?Python必不可少
- Python培训出来真的好找工作吗?好找工作的关键是什么?
- Python协程 Gevent Eventlet Greenlet
- 我为什么从python转向go
- 为了让大家更好地学习python爬虫,我们做了一个“靶子”
- python开发岗位需求分析,来看看它是什么一个情况吧
- OpenCV-Python实战(5) —— OpenCV 使用cv.setMouseCallback实现截图功能
- 【Python实战】 ---- python 实现 CSDN 的定时自动签到
- 简单Python爬虫编写——requests包使用初体验