Python 中的默认值是什么?

pythonserver side programmingprogramming更新于 2025/4/29 5:00:17

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

默认值表示如果在函数调用期间未提供参数值,则函数参数将采用该值。默认值是使用赋值运算符 (=) 分配的,形式为 keywordname=value。

示例

# 通过提供默认值创建函数
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 个位置参数(网站、作者、语言)的函数。 author 和 standard 参数的值从默认值更改为新的传递值。

使用关键字参数的函数调用

示例

# 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

解释

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

  • 第二次调用中,一个参数是必需的,一个是可选的(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 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

关键字参数之后,author (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 函数中的默认值。我们通过一些示例了解了默认参数和参数。


相关文章


有用资源