[英]How can I denote unused function arguments?
When "deconstructing" a tuple, I can use _
to denote tuple elements I'm not interested in, eg当“解构”一个元组时,我可以使用_
来表示我不感兴趣的元组元素,例如
>>> a,_,_ = (1,2,3)
>>> a
1
Using Python 2.x, how can I express the same with function arguments?使用 Python 2.x,如何表达与 function arguments 相同的内容? I tried to use underscores:我尝试使用下划线:
>>> def f(a,_,_): return a
...
File "<stdin>", line 1
SyntaxError: duplicate argument '_' in function definition
I also tried to just omit the argument altogether:我也试图完全省略这个论点:
>>> def f(a,,): return a
File "<stdin>", line 1
def f(a,,): return a
^
SyntaxError: invalid syntax
Is there another way to achieve the same?还有另一种方法可以达到同样的目的吗?
A funny way I just thought of is to delete the variable:我刚刚想到的一个有趣的方法是删除变量:
def f(foo, unused1, unused2, unused3):
del unused1, unused2, unused3
return foo
This has numerous advantages:这有很多优点:
del
is the solution recommended in the PyLint manual . PyLint 不会抱怨,使用del
是PyLint 手册中推荐的解决方案。The underscore is used for things we don't care about and the * in *args denotes a list of arguments. Therefore, we can use *_ to denote a list of things we don't care about:下划线用于我们不关心的东西,*args中的*表示arguments的列表。因此,我们可以用*_来表示我们不关心的东西的列表:
def foo(bar, *_):
return bar
It even passes PyCharm's checks.它甚至通过了 PyCharm 的检查。
You can use '_' as prefix, so that pylint will ignore these parameters:您可以使用 '_' 作为前缀,这样 pylint 将忽略这些参数:
def f(a, _b, _c):
Here's what I do with unused arguments:这是我对未使用的 arguments 所做的操作:
def f(a, *unused):
return a
In order to avoid "unused variable" inspection messages for unused *args and/or **kwargs, I replace args
and kwargs
by _
and __
:为了避免未使用的 *args 和/或 **kwargs 的“未使用变量”检查消息,我将args
和kwargs
替换为_
和__
:
def f(a, b, *_, **__):
...
In addition to remove messages, it clearly shows that you don't care about these arguments.除了删除消息,它清楚地表明你不关心这些 arguments。
I can't say if it is a really universal solution, but it worked everywhere I've used it until now.我不能说它是否是一个真正通用的解决方案,但它在我使用过的所有地方都有效,直到现在。
If you have both args and keyword arg you should use如果您同时拥有 args 和关键字 arg,则应使用
def f(a, *args, **kwargs):
return a
I think the accepted answer is bad, but it can still work, if you use what I should call "Perl way" of dealing with arguments (I don't know Perl really, but I quit trying to learn it after seeing the sub
syntax, with manual argument unpacking):我认为接受的答案是不好的,但它仍然可以工作,如果你使用我应该称之为“Perl 方式”来处理 arguments(我真的不知道 Perl,但在看到sub
语法后我不再尝试学习它,手动解包参数):
Your function has 3 arguments - this is what it gets called with (Duck typing, remember?).你的 function 有 3 arguments - 这就是它的名字(鸭子打字,还记得吗?)。 So you get:所以你得到:
def funfun(a, b, c):
return b * 2
2 unused parameters. 2个未使用的参数。 But now, enter improved larsmans' approach:但是现在,输入改进的 larsmans 方法:
def funfun(*args):
return args[1] * 2
And there go the warnings...还有 go 警告...
However, I still enjoy more the boxed's way:不过,我还是更喜欢盒装的方式:
def funfun(a, b, c):
del a, c
return b * 2
It keeps the self-documenting quality of parameter names.它保持参数名称的自记录质量。 They're a feature, not a bug.它们是一个特性,而不是一个错误。
But, the language itself doesn't force you there - you could also go the other way around, and just let all your function have the signature (*args, **kwargs)
, and do the argument parsing manually every time.但是,语言本身并没有强迫你在那里 - 你也可以 go 相反,让你所有的 function 都有签名(*args, **kwargs)
,每次都手动解析参数。 Imagine the level of control that gives you.想象一下给您的控制级别。 And no more exceptions when being called in a deprecated way after changing your "signature" (argument count and meaning).在更改“签名”(参数计数和含义)后以不推荐的方式调用时不再有异常。 This is something worth considering;)这是值得考虑的事情;)
You can use "*_" to use multiple unused arguments可以用“*_”来使用多个未使用的arguments
def test(x, *_):
return x
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.