js有默认参数的函数加参数_函数参数:默认,关键字和任意
js有默認(rèn)參數(shù)的函數(shù)加參數(shù)
PYTHON開發(fā)人員的提示 (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.
假設(shè)您正在編寫一個(gè)接受多個(gè)參數(shù)的函數(shù),并且其中某些參數(shù)通常具有一個(gè)公共值。 例如,您希望能夠在不顯式定義每個(gè)參數(shù)的情況下調(diào)用該函數(shù)。 換句話說,您希望某些參數(shù)具有默認(rèn)參數(shù)。
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.
接下來,將向您介紹如何編寫具有默認(rèn)參數(shù)和靈活參數(shù)的函數(shù),該函數(shù)允許您將任意數(shù)量的參數(shù)傳遞給函數(shù)。
先決條件 (Prerequisites)
If you do not familiar with defining your own function, the article below will give you more information about it.
如果您不熟悉定義自己的函數(shù),則下面的文章將為您提供有關(guān)它的更多信息。
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.
首先,用默認(rèn)參數(shù)值定義一個(gè)函數(shù)。 在函數(shù)頭中,我們將感興趣的參數(shù)附加等號和默認(rèn)參數(shù)值。 請注意,此函數(shù)將第一個(gè)參數(shù)計(jì)算為第二個(gè)參數(shù)的乘方,第二個(gè)默認(rèn)參數(shù)的默認(rèn)值為1。這意味著我們可以像您期望的那樣使用兩個(gè)參數(shù)調(diào)用該函數(shù)。 但是,如果僅使用一個(gè)參數(shù),則函數(shù)調(diào)用將對第二個(gè)參數(shù)使用默認(rèn)參數(shù)1。
Through this article, you will gain expertise in writing functions with both single and multiple default arguments.
通過本文,您將獲得使用單個(gè)和多個(gè)默認(rèn)參數(shù)編寫函數(shù)的專業(yè)知識(shí)。
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.
現(xiàn)在讓我們看一下靈活的參數(shù)。 假設(shè)您要編寫一個(gè)函數(shù),但是不確定用戶要傳遞多少個(gè)參數(shù)。 例如,一個(gè)函數(shù)接受浮點(diǎn)數(shù)或整數(shù),并將它們加起來。 這稱為靈活參數(shù)。
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.
在此示例中,我們編寫了對所有傳遞給它的參數(shù)求和的函數(shù)。 在函數(shù)定義中,我們使用參數(shù)*args ,然后將傳遞給函數(shù)調(diào)用的所有參數(shù)轉(zhuǎn)換為函數(shù)體內(nèi)的元組。 如果使用此方法*args ,則在調(diào)用函數(shù)時(shí)該參數(shù)沒有最大值。
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.
您還可以使用**傳遞任意數(shù)量的關(guān)鍵字參數(shù)kwargs 。 也就是說,參數(shù)前面有標(biāo)識(shí)符。 我們將編寫一個(gè)名為print_all的函數(shù),該函數(shù)打印出標(biāo)識(shí)符和傳遞給它們的參數(shù)。 要編寫這樣的函數(shù),我們使用參數(shù)** kwargs 。 這會(huì)將標(biāo)識(shí)符關(guān)鍵字對變成功能體內(nèi)的字典。 然后,在函數(shù)主體中,我們需要打印存儲(chǔ)在字典kwargs中的所有鍵值對。
For your reference, we can use args and kwargs in combination. Let’s see the following example here.
供您參考,我們可以結(jié)合使用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.
使用靈活參數(shù)時(shí),名稱args和kwargs不是必需的。 您可以定義任何想要的變量名。 但是,它們必須分別以*和**開頭。 我知道這需要很多,所以是時(shí)候自己破解了。
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?關(guān)于作者 (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是一位研究人員,負(fù)責(zé)收集,組織和分析意見和數(shù)據(jù)以解決問題,探索問題和預(yù)測趨勢。
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.
他幾乎在機(jī)器學(xué)習(xí)和深度學(xué)習(xí)的每個(gè)領(lǐng)域工作。 他正在許多領(lǐng)域進(jìn)行實(shí)驗(yàn)和研究,包括卷積神經(jīng)網(wǎng)絡(luò),自然語言處理和遞歸神經(jīng)網(wǎng)絡(luò)。
Connect on LinkedIn
在 LinkedIn上 連接
翻譯自: https://towardsdatascience.com/function-arguments-default-keyword-and-arbitrary-9588b5eaaef3
js有默認(rèn)參數(shù)的函數(shù)加參數(shù)
總結(jié)
以上是生活随笔為你收集整理的js有默认参数的函数加参数_函数参数:默认,关键字和任意的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据科学还是计算机科学_数据科学101
- 下一篇: 相似邻里算法_纽约市-邻里之战