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.
如果您不熟悉定義自己的函數,則下面的文章將為您提供有關它的更多信息。
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.
現在讓我們看一下靈活的參數。 假設您要編寫一個函數,但是不確定用戶要傳遞多少個參數。 例如,一個函數接受浮點數或整數,并將它們加起來。 這稱為靈活參數。
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 ,則在調用函數時該參數沒有最大值。
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。 讓我們在這里看下面的例子。
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.
使用靈活參數時,名稱args和kwargs不是必需的。 您可以定義任何想要的變量名。 但是,它們必須分別以*和**開頭。 我知道這需要很多,所以是時候自己破解了。
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有默認參數的函數加參數
總結
以上是生活随笔為你收集整理的js有默认参数的函数加参数_函数参数:默认,关键字和任意的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据科学还是计算机科学_数据科学101
- 下一篇: 梦到黑无常预示着什么