python中的nargin功能(来自Matlab) [英] nargin functionality (from Matlab) in Python

查看:171
本文介绍了python中的nargin功能(来自Matlab)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试翻译以下代码行:

I am attempting to translate these lines of code:

error(nargchk(3, 4, nargin))
if nargout > 2, error('too many output parameters'); end

我想将其翻译成Python.我还没有找到等效的错误(尽管我也许可以在这里抛出异常?),nargchk和nargin.有人可以提供有关此代码行如何翻译的任何输入吗?

I want to translate it into Python. I have yet to find an equivalent for error (although I could perhaps just throw an exception here?), nargchk and nargin. Can anybody provide any input as to how this line of code can be translated?

推荐答案

没有什么等效于 nargin nargchk 的东西,因为Python不需要它.您可以在函数定义中直接在Python中定义可选参数.例如:

There is nothing equivalent to nargin and nargchk because Python doesn't need it. You can define optional arguments in Python directly in the function definition. So for example:

def myfunc(a=1, b=3):

在这种情况下,您可以将 a b 设置为所需的任何值,但是,如果未定义它们,它将使用您指定的默认值.因此,如果您调用 myfunc(),则会将 a 设置为 1 ,将 b 设置为3.code> myfunc(5),它将 a 设置为 5 ,将 b 设置为 3 .您还可以按名称进行呼叫,例如 myfunc(b = 10),它将 a 设置为 1 b 到10.

In this case it lets you set a and b to whatever you want, but if you don't define them it uses the default values you specified. So if you call myfunc(), it sets a to 1 and b to 3. If you call myfunc(5) it sets a to 5 and b to 3. You can also call by name, such as myfunc(b=10), which sets a to 1 and b to 10.

如果您想要参数,则不要在函数定义中为其分配任何内容.因此,在以下情况下, a 是必需的,而 b 是可选的:

If you want argument to be required, just don't assign anything to them in the function definition. So in the following case, a is required while b is optional:

def myfunc(a, b=3):

如果希望用户能够指定任意数量的参数,则可以使用 * args (其中 args 可以是您想要的任何变量.因此,:

If you want a user to be able to specify an arbitrary number of arguments, you can use *args (where args can be whatever variable you want. So this:

def myfunc(a=1, b=3, *args):

将第一个参数分配给 a ,将第二个参数分配给 b ,并将所有剩余的参数分配给 args .您也可以使用 ** kwargs 通过名称定义的参数来执行此操作(同样, kwargs 可以是任何变量名).因此:

Will assign the first argument to a, the second argument to b, and any remaining arguments to args. You can also do this with arguments defined by name using **kwargs (again, kwargs can be any variable name). So this:

def myfunc(a=1, b=3, *args, **kwargs):

与上一个相同,但除 a b 之外,名称提供的任何参数都将放入 kwargs (是字典).

Does the same thing as the previous one, but any arguments provided by name other than a and b will go in kwargs (which is a dictionary).

因此,您不需要 nargin nargchk ,通常只需定义所需的参数并为可选参数设置默认值即可.然后,Python将自动进行检查,以确保指定了必需的选项,并且没有提供太多的参数.如果需要捕获任意数量的其他参数,只需使用 * args ** kwargs ,然后可以检查 len 是否正确你自己.

So you don't need nargin and nargchk, generally you can just define the arguments you need and set default values for the optional ones. Then Python will automatically handle checking to make sure the required options are specified and there aren't too many arguments provided. If you need to capture an arbitrary number of additional arguments, just use *args and **kwargs and you can check that the len is correct yourself.

对于 nargout ,没有python等效项,因为python要求处理所有输出.因此,如果执行 return a,b ,则必须同时处理 myfun 中的 a b .您可以简单地忽略某些内容,但是必须明确地执行此操作.没有简单的方法来检查这些参数是否被忽略.所以说我们在 myfunc 中有这个:

For nargout, there is no python equivalent because python requires all outputs be handled. So if you do return a, b, in myfun, a, and b must both be handled. You can simply ignore some, but you must explicitly do this. There is no easy way to check if those arguments are being ignored. So say we have this in myfunc:

return a, b, c, d

您只需要 a ,您可以这样做:

And you only want a, you can just do:

a, *_ = myfunc()

这会将第一个返回的值放在 a 中,并将其余的值转储到一次性变量 _ 中(就像 * args 捕获所有其余参数, * _ 捕获所有剩余输出).例如,对于 a c ,您可以执行以下操作:

This puts the first returned value in a and dumps the remainder in the throw-away variable _ (just like *args captures all remaining arguments, *_ captures all remaining outputs). For, say, a and c, you can do:

a, _, c, _ = myfunc()

您也可以直接从函数调用中建立索引,而MATLAB不允许这样做.因此这也是可能的.因此,要获得第一个参数,您可以执行以下操作:

You can also index directly from a function call, something MATLAB doesn't allow. So this is also possible. So to get the first argument, you can do:

a = myfunc()[0]

因此,由于在Python中很容易获得所需的返回值,并且由于输出值的隐式变量数很难正确处理,因此Python不支持它.如果您确实想更改输出参数的数量,可以在函数定义中设置一个可选的命名参数.由于可选参数在Python中很简单,因此这不是一个大问题.但这通常不是必需的.

So since it is easy in Python to get the returned values you want, and since implicit variable numbers of output values is an extreme pain to handle properly, Python doesn't support it. If you really want to change the number of output argument you can set an optional named argument in the function definition to do so. Since optional arguments are easy in Python, this isn't a huge issue. But it isn't usually needed.

对于错误,是的,您只是 raise 相应的异常.Python在不同情况下具有不同类型的错误.对于错误数量的参数, raise TypeError('blah')是正确的方法,其中 blah 是您想要的消息.

As for errors, yes you just raise the appropriate exception. Python has different types of errors for different situations. raise TypeError('blah') is the correct approach for the wrong number of arguments, where blah is the message you want.

这篇关于python中的nargin功能(来自Matlab)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆