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 個位置引數(website、author、language)呼叫函式。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

在關鍵字引數之後,有一個非關鍵字引數 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'

因為關鍵字 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 函式中的預設值。我們透過一些示例瞭解了預設引數。

更新於: 2022 年 9 月 15 日

15K+ 瀏覽量

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告