js有默認參數的函數加參數_函數參數:默認,關鍵字和任意

js有默認參數的函數加參數

PYTHON開發人員的提示 (TIPS FOR PYTHON DEVELOPERS)

Think that you are writing a function that accepts multiple parameters, and there is often a common value for some of these parameters. For instance, you would like to be able to call the function without explicitly defining every parameter. In other words, you would like some parameters to have default arguments.

假設您正在編寫一個接受多個參數的函數,并且其中某些參數通常具有一個公共值。 例如,您希望能夠在不顯式定義每個參數的情況下調用該函數。 換句話說,您希望某些參數具有默認參數。

Next, you will be introduced to how to write a function with default arguments and flexible arguments, which allows you to pass any number of arguments to a function.

接下來,將向您介紹如何編寫具有默認參數和靈活參數的函數,該函數允許您將任意數量的參數傳遞給函數。

先決條件 (Prerequisites)

If you do not familiar with defining your own function, the article below will give you more information about it.

如果您不熟悉定義自己的函數,則下面的文章將為您提供有關它的更多信息。

Image for post
Author作者

First, to define a function with a default argument value. In the function header, we attach the parameter of interest with an equals sign and the default argument value. Notice that this function calculates the first argument to the power of the second argument, and the default second argument value is 1. This means we can call the function with two arguments as you would expect. However, if you only use one argument, the function call will use the default argument of 1 for the second parameter.

首先,用默認參數值定義一個函數。 在函數頭中,我們將感興趣的參數附加等號和默認參數值。 請注意,此函數將第一個參數計算為第二個參數的乘方,第二個默認參數的默認值為1。這意味著我們可以像您期望的那樣使用兩個參數調用該函數。 但是,如果僅使用一個參數,則函數調用將對第二個參數使用默認參數1。

Through this article, you will gain expertise in writing functions with both single and multiple default arguments.

通過本文,您將獲得使用單個和多個默認參數編寫函數的專業知識。

Let’s now look at flexible arguments. Say that you want to write a function but aren’t sure how many arguments a user will want to pass. For example, a function that takes floats or ints, and sums them all up. This is called a flexible argument.

現在讓我們看一下靈活的參數。 假設您要編寫一個函數,但是不確定用戶要傳遞多少個參數。 例如,一個函數接受浮點數或整數,并將它們加起來。 這稱為靈活參數。

Image for post
Author作者

In this example, we write the function that sums up all the arguments passed to it. In the function definition, we use the parameter *args, then turns all the arguments passed to a function call into a tuple in the function body. There is no maximum value for the parameter when we call the function if we use this method, *args.

在此示例中,我們編寫了對所有傳遞給它的參數求和的函數。 在函數定義中,我們使用參數*args ,然后將傳遞給函數調用的所有參數轉換為函數體內的元組。 如果使用此方法*args ,則在調用函數時該參數沒有最大值。

Image for post
Author作者

You can also use a ** to pass an arbitrary number of keyword arguments called kwargs. That is, arguments preceded by identifiers. We will write such a function called print_all that prints out the identifiers and the parameters passed to them. To write such a function, we use the parameter kwargs preceded by a **. This turns the identifier-keyword pairs into a dictionary within the function body. Then, in the function body, we need to print all the key-value pairs stored in the dictionary kwargs.

您還可以使用**傳遞任意數量的關鍵字參數kwargs 。 也就是說,參數前面有標識符。 我們將編寫一個名為print_all的函數,該函數打印出標識符和傳遞給它們的參數。 要編寫這樣的函數,我們使用參數** kwargs 。 這會將標識符關鍵字對變成功能體內的字典。 然后,在函數主體中,我們需要打印存儲在字典kwargs中的所有鍵值對。

For your reference, we can use args and kwargs in combination. Let’s see the following example here.

供您參考,我們可以結合使用args和kwargs。 讓我們在這里看下面的例子。

Image for post
Author作者

The names args and kwargs are not essential when using flexible arguments. You can define any variable name you want to. However, they must be preceded by a * and **, respectively. I know this is a lot to take in, so it’s time to hack it out yourself.

使用靈活參數時,名稱argskwargs不是必需的。 您可以定義任何想要的變量名。 但是,它們必須分別以***開頭。 我知道這需要很多,所以是時候自己破解了。

Other Interesting Articles#1 Writing Your Own Functions#2 Scope of Variable and LEGB Rule#3 Python: Procedural or Object-Oriented Programming?#4 Data Science with Python: How to Use NumPy Library#5 Do you have the Software Engineer and Data Scientist skills?

關于作者 (About the Author)

Wie Kiang is a researcher who is responsible for collecting, organizing, and analyzing opinions and data to solve problems, explore issues, and predict trends.

Wie Kiang是一位研究人員,負責收集,組織和分析意見和數據以解決問題,探索問題和預測趨勢。

He is working in almost every sector of Machine Learning and Deep Learning. He is carrying out experiments and investigations in a range of areas, including Convolutional Neural Networks, Natural Language Processing, and Recurrent Neural Networks.

他幾乎在機器學習和深度學習的每個領域工作。 他正在許多領域進行實驗和研究,包括卷積神經網絡,自然語言處理和遞歸神經網絡。

Connect on LinkedIn

LinkedIn上 連接

翻譯自: https://towardsdatascience.com/function-arguments-default-keyword-and-arbitrary-9588b5eaaef3

js有默認參數的函數加參數

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

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

相關文章

sql management studio 附加mdf文件出錯的解決辦法

將mdf文件所在文件夾的權限改為everyone.,完全控制即可。

oracle raise_application_error,RAISE_ APPLICATION_ ERROR--之異常處理

平時用來測試的異常處理我們都是通過dbms_output.put_line來輸出異常信息,但是在實際的應用中,需要把異常信息返回給調用的客戶端。其實 RAISE_APPLICATION_ERROR 是將應用程序專有的錯誤從服務器端轉達到客戶端應用程序(其他機器上的SQLPLUS或者其他前臺…

金融信息交換協議

隨著網絡的使用,目前所有大型的金融機構都已經實現了自動化和數字化。當中肯定少不了互聯網的加入,那么在這當中,我們主要介紹一下FIX協議。它是由國際FIX協會組織提供的一個開放式協議,目的是推動國際貿易電子化的進程&#xff0…

2018大數據學習路線從入門到精通

最近很多人問小編現在學習大數據這么多,他們都是如何學習的呢。很多初學者在萌生向大數據方向發展的想法之后,不免產生一些疑問,應該怎樣入門?應該學習哪些技術?學習路線又是什么?今天小編特意為大家整理了…

相似鄰里算法_紐約市-鄰里之戰

相似鄰里算法IBM Data Science Capstone ProjectIBM Data Science Capstone項目 分析和可視化與服裝店投資者的要求有關的紐約市結構 (Analyzing and visualizing the structure of New York City in relation to the requirements of a Clothing Store Investor) 介紹 (Introd…

一、面向對象

第一節:面向對象編程1.面向對象三大原則:封裝:就是把客觀事物封裝成抽象的類,并且類可以把自己的數據和方法只讓可信的類或者對象操作,對不可信的進行信息隱藏。繼承:繼承,指可以讓某個類型的對…

[poj 1364]King[差分約束詳解(續篇)][超級源點][SPFA][Bellman-Ford]

題意 有n個數的序列, 下標為[1.. N ], 限制條件為: 下標從 si 到 sini 的項求和 < 或 > ki. 一共有m個限制條件. 問是否存在滿足條件的序列. 思路 轉化為差分約束, 就是 即 Si 為第 i 項的前綴和, 特別的 So 為0. 轉化不等式(連續子段和變為前綴和之差 > < 變為 &g…

linux質控命令,Linux下microRNA質控-cutadapt安裝

如果Linux系統已安裝pip或conda&#xff0c;cutadapt的安裝相對簡便一些&#xff0c;示例如下&#xff1a;1.pip安裝pip install --user --upgrade cutadapt添加環境變量echo export PATH$PATH:/your path/cutadapt-1.10/bin >> ~/.bashrc2.conda安裝conda install -c b…

采用多播傳送FIX行情數據的推薦方案

理由FIX協議由一個會話層協議&#xff0c;一個應用層協議和一套域數據字典組成。后兩者不依賴于FIX會話。而且&#xff0c;由于FIX會話作為Point-to-point&#xff08;點-對-點&#xff09;通信&#xff0c;并不適合于發布/訂閱模式&#xff08;如為大量接收者提供市場數據&…

AJAX 異步加載技術

AJAX 異步 JavaScript 和 XML。 AJAX 是一種用于創建快速動態網頁的技術。 通過在后臺與服務器進行少量數據交換&#xff0c;AJAX 可以使網頁實現異步更新。這意味著可以在不重新加載整個網頁的情況下&#xff0c;對網頁的某部分進行更新。 傳統的網頁&#xff08;不使用 AJAX…

linux分辨率和用戶有關嗎,Linux系統在高分屏非正常分辨率顯示

問題描述&#xff1a;win10重裝為Ubuntu16.04&#xff0c;在1920x1080的顯示屏上&#xff0c;linux系統分辨率只有800x600xrandr # 查看當前顯示分辨率#輸出&#xff1a;[Screen 0: minimum 800 x 600, current 800 x 600, maximum 800 x 600]可以看出顯示屏最小為800x600&…

數據透視表和數據交叉表_數據透視表的數據提取

數據透視表和數據交叉表Consider the data of healthcare drugs as provided in the excel sheet. The concept of pivot tables in python allows you to extract the significance from a large detailed dataset. A pivot table helps in tracking only the required inform…

金融信息交換協議(FIX)v5.0

1. 什么是FIXFinancial Information eXchange(FIX)金融信息交換協議的制定是由多個致力于提升其相互間交易流程效率的金融機構和經紀商于1992年共同發起。這些企業把他們及他們的行業視為一個整體&#xff0c;認為能夠從對交易指示&#xff0c;交易指令及交易執行的高效電子數…

觀光公交

【問題描述】 風景迷人的小城 Y 市&#xff0c;擁有 n 個美麗的景點。由于慕名而來的游客越來越多&#xff0c;Y 市特意安排了一輛觀光公交車&#xff0c;為游客提供更便捷的交通服務。觀光公交車在第 0 分鐘出現在 1 號景點&#xff0c;隨后依次前往 2、3、4……n 號景點。從…

linux行命令測網速,Linux命令行測試網速的方法

最近給服務器調整了互聯網帶寬的限速策略&#xff0c;調到100M讓自己網站也爽一下。一般在windows上我喜歡用speedtest.net來測試&#xff0c;測速結果也被大家認可。在linux上speedtest.net提供了一個命令行工具speedtest-cli&#xff0c;用起來很方便&#xff0c;這里分享一下…

Delphi XE2獲取漢字拼音首字母

function CnPYIndex(const CnString: string): string;{ 返回中文的拼音首字母}const ChinaCode: array[0..25, 0..1] of Integer ((1601, 1636), (1637, 1832), (1833, 2077), (2078, 2273), (2274, 2301), (2302, 2432), (2433, 2593), (2594, 2786), (9999, 0000), …

圖像處理傅里葉變換圖像變化_傅里葉變換和圖像床單視圖。

圖像處理傅里葉變換圖像變化What do Fourier Transforms do? What do the Fourier modes represent? Why are Fourier Transforms notoriously popular for data compression? These are the questions this article aims to address using an interesting analogy to repre…

HDUOJ 1062 TEXT REVERSE

#include<iostream> #include<stdlib.h> #include <iomanip> #include<stack> using namespace std;int main(){//次數int n 0;while (cin >> n) {//這里需要讀一個字符&#xff0c;需要消除換行符的影響getchar();while (n--) {char c;stack&l…

VC++的windows服務

項目建立&#xff1a; VS2008->ATL項目->服務exe 服務部署&#xff1a; 在VS2008中新建的ATL項目&#xff0c;在網上看到的流程中都會有注冊服務這一項。 但是對服務的注冊都是只給了一個指令&#xff0c;一句話帶過&#xff0c;沒有給出具體的操作步驟 經過多次試驗…

linux中gradle編譯慢,【Linux】解決linux下android studio用gradle構建從jcenter或maven下載依賴太慢...

一個簡單的辦法&#xff0c;修改項目根目錄下的build.gradle&#xff0c;將jcenter()或者mavenCentral()替換掉即可&#xff1a;allprojects {repositories {maven{ url http://maven.oschina.net/content/groups/public/}}}但如果你有很多個項目的話&#xff0c;一個一個的改估…