Python默認參數

Python | 默認參數 (Python | default parameters)

A default parameter is a value provided in a function declaration that is automatically assigned by the compiler if the caller of the function doesn't provide a value for the parameter with the default value.

默認參數是函數聲明中提供的值,如果函數的調用者不提供默認值的參數值,則編譯器自動為其賦值

Following is a simple Python example to demonstrate the use of default parameters. We don't have to write 3 Multiply functions, only one function works by using default values for 3rd and 4th parameters.

以下是一個簡單的Python示例,以演示默認參數的用法 。 我們沒有使用默認值3 4點的參數寫3個乘功能,只有一種功能的作品。

Code:

碼:

# A function with default arguments, it can be called with 
# 2 arguments or 3 arguments or 4 arguments .
def Multiply( num1, num2, num3 = 5, num4 = 10 ):
return num1 * num2 * num3 * num4
# Main code
print(Multiply(2,3))
print(Multiply(2,3,4))
print(Multiply(2,3,4,6))

Output

輸出量

300
240
144

Key points:

關鍵點:

1) Default parameters are different from constant parameters as constant parameters can't be changed whereas default parameters can be overwritten if required.

1) 默認參數與常量參數不同,因為不能更改常量參數,而如果需要可以覆蓋默認參數

2) Default parameters are overwritten when the calling function provides values for them. For example, calling of function Multiply(2, 3, 4, 6) overwrites the value of num3 and num4 to 4 and 6 respectively.

2)調用函數為其提供默認值時, 默認參數將被覆蓋。 例如,調用函數Multiply( 2,3,4,6 ) 會將num3和num4的值分別覆蓋為46

3) During calling of function, arguments from calling a function to parameters of the called function are copied from left to right. Therefore, Multiply(2, 3, 4) will assign 2, 3 and 4 to num1, num2, and num3. Therefore, the default value is used for num4 only.

3)在調用函數期間,從調用函數到被調用函數的參數的參數從左到右復制。 因此, 乘(2,3,4)將分配2,34至NUM1,NUM2和NUM3。 因此,默認值僅用于num4 。

翻譯自: https://www.includehelp.com/python/default-parameters.aspx

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/540671.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540671.shtml
英文地址,請注明出處:http://en.pswp.cn/news/540671.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

最長公共前綴_最長的公共前綴

最長公共前綴Problem statement: 問題陳述: Write a function to find the longest common prefix string amongst an array of strings. 編寫函數以在字符串數組中找到最長的公共前綴字符串 。 If there is no common prefix, return an empty string "&quo…

物聯網聽起來像是一個和互聯網不同的網,萬物互聯又把網給弄丟了,正向我們撲面而來的是萬物互聯網。...

物聯網聽起來像是一個和互聯網不同的網,"萬物互聯"又把"網"給弄丟了,正向我們撲面而來的是"萬物互聯網"。轉載于:https://www.cnblogs.com/beingonline/p/7484135.html

sdram trp_TRP的完整形式是什么?

sdram trpTRP:電視收視點 (TRP: Television Rating Point) TRP is an abbreviation of "Television Rating Point". TRP是“電視評分點”的縮寫 。 It is a system or standard of measurement which signifies the demand and popularity of a televisi…

Controller計算值傳到jsp頁面,用session傳值

HttpSession session request.getSession(); session.setAttribute("key",value); jap 用 ${key}來接收該值 轉載于:https://www.cnblogs.com/douder/p/7484491.html

CBT的完整形式是什么?

CBT:基于計算機的培訓 (CBT: Computer Based Training) CBT is an abbreviation of "Computer-based training". CBT是“基于計算機的培訓”的縮寫 。 It is a training program which entails the use of a personal system or networked computer. The…

論道社會化商業

主持人 用友優普副總裁傅毅: 謝謝各位嘉賓,我們會留一些時間讓在座的嘉賓提問。請各位嘉賓用一個非常簡單的一句話,或者幾個關鍵詞,總結一下你認為的社會化商業是什么? 用友優普執行總裁 徐洋: 社會化商業為…

CChelper彩虹SDK可視遠程客服解決方案

本文講的是 : CChelper彩虹SDK可視遠程客服解決方案 , 在智能生態產業鏈中,智能硬件終端是把握消費者的直接環節,隨著物聯網時代邁向成熟,智能家居領域的硬件逐漸成為智能硬件終端的主角。目前的市場環境下,智能家居領域的自身硬…

matlab 簡介_MATLAB簡介

matlab 簡介MATLAB簡介 (MATLAB Introduction) MATLAB was designed by Cleve Moler for his student in 1970s but after some time jack little, an engineer realized its potential and rewrote it at the MathWorks, and it was rewritten in C language by the date of 1…

Scala中的嵌套循環

Scala中的嵌套循環 (Nested loop in Scala) In programming, a nested loop is used in initializing or iterate multi-dimensional array or to print patterns. Scala provides an efficient method to use nested loops in the programming language. The most used nested…

python基礎-字典

字典 # 字典是python基本數據結構之一,相對于列表和元組,他是無序的,每次輸出都打亂了順序,沒有下標hello{110:{"name":"alex","age":28,"home":"shandong"},111:{"name&…

sql算術運算符_SQL中的算術運算符

sql算術運算符SQL | 算術運算符 (SQL | Arithmetic Operators) Different number-crunching administrators are utilized in SQL to be specific Addition (), Subtraction (-), Multiplication (*), Division (/), Modulus (%). SQL中使用了不同的數字運算管理員來表示特定的…

HDU 6188 Duizi and Shunzi

棧。 將數字排序后&#xff0c;一個一個壓入棧。如果棧頂兩個元素形成了對子&#xff0c;那么$ans1$&#xff0c;彈出棧頂兩個元素&#xff1b;如果棧頂三個元素形成了順子&#xff0c;那么$ans1$&#xff0c;彈出棧頂三個元素。 #include<bits/stdc.h> using namespace …

php 單例模式有什么缺點_PHP的完整形式是什么?

php 單例模式有什么缺點PHP&#xff1a;超文本預處理器 (PHP: Hypertext Preprocessor ) PHP is an abbreviation of Hypertext Preprocessor, earlier called Personal Home Page. PHP is extensively used HTML-embedded, open-source server-side scripting language create…

Myeclipse有關的問題

Myeclipse配置問題 1.行數顯示 window ----preference----General-----Editors-----TextEditors----show line numbers 2.編碼設置 window ---preference----workspace-----設置 3.jsp編碼設置 window ---preference----myeclipse------Files And Editors------jsp 4.jsp的視圖…

weak-to-strong-generalization始終比母體更智能的人工智能,能否被它的母體所監管supervision,從而變的更強

正如supervison這個詞&#xff0c;就像就是母親對孩子的超級super愿景vision&#xff0c;比母親更聰明更強&#xff0c;也就意味著要按照母親期望的那樣成長&#xff0c;不合理的行為要能夠糾正supervison。 一代比一代強&#xff0c;一代比一代好。 弱模型監督能否激發出更強…

最小跳數

Description: 描述&#xff1a; This problem is a standard interview problem which has been featured in interview rounds of Adobe, Amazon, Oyo rooms etc. 此問題是標準的采訪問題&#xff0c;已在Adobe&#xff0c;Amazon&#xff0c;Oyo房間等的采訪回合中出現。 P…

《Web安全之機器學習入門》一 第3章 機器學習概述

第3章 機器學習概述機器學習的概念非常多&#xff0c;從有監督到無監督&#xff0c;從聚類到回歸&#xff0c;從淺層學習到深度學習&#xff0c;從準確率到召回率&#xff0c;它們究竟是什么意思呢&#xff1f;本章將介紹最主要的幾個概念。不少機器學習初學者甚至包括業內老司…

ue 抗鋸齒 渲染序列失靈_最大的鋸齒形序列

ue 抗鋸齒 渲染序列失靈Problem statement: 問題陳述&#xff1a; Given a square matrix of size n x n, find the sum of the Zigzag sequence with the largest sum. A zigzag sequence starts from the top and ends at the bottom. Two consecutive elements of sequence…

團隊-團隊編程項目作業名稱-成員簡介及分工

成員&#xff1a;祁昊 分工&#xff1a;ui設計&#xff0c;美工&#xff0c;詳細設計。轉載于:https://www.cnblogs.com/qihao10086/p/7496101.html

python身份運算符_Python身份運算符

python身份運算符Identity operators are used to perform the comparison operation on the objects i.e. these operators check whether both operands refer to the same objects (with the same memory location) or not. 身份運算符用于對對象執行比較操作&#xff0c;即…