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不是必需的。 您可以定义任何想要的变量名。 但是,它们必须分别以***开头。 我知道这需要很多,所以是时候自己破解了。

关于作者 (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有默认参数的函数加参数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值