Python 默认值是什么

Python 默认值是什么

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

默认值表示,如果在函数调用过程中没有给出参数值,函数参数将采用该值。默认值是通过使用形式为 keywordname=value(=) 赋值运算符来进行赋值的。

示例

# 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个位置参数(网站,作者,语言)调用函数。参数 authorstandard 的值从默认值更改为新的传递值。

使用关键字参数进行函数调用

示例

# 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

在关键字参数之后,为作者( 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'

因为在函数中未定义关键字”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函数中的默认值。通过一些示例,我们了解了默认参数和参数。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程