我怎样才能表示未使用的函数参数?

时间:2021-08-23 20:14:08

When "deconstructing" a tuple, I can use _ to denote tuple elements I'm not interested in, e.g.

当“解构”一个元组时,我可以使用_来表示我不感兴趣的元组元素,例如:

>>> a,_,_ = (1,2,3)
>>> a
1

Using Python 2.x, how can I express the same with function arguments? I tried to use underscores:

使用Python 2.x,如何用函数参数表达相同的内容?我试着使用下划线:

>>> 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?

还有另一种方法可以达到同样的目的吗?

6 个解决方案

#1


20  

Here's what I do with unused arguments:

这是我对未使用的参数所做的事情:

def f(a, *unused):
    return a

#2


59  

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:

这有很多好处:

  • The unused variable can still be used when calling the function both as a positional argument and as a keyword argument.
  • 在将函数作为位置参数和关键字参数调用时,仍然可以使用未使用的变量。
  • If you start to use it later, you can't since it's deleted, so there is less risk of mistakes.
  • 如果你以后开始使用它,你就不能删除,因此错误的风险会降低。
  • It's standard python syntax.
  • 这是标准的python语法。
  • PyCharm does the right thing!
  • PyCharm做对了!
  • PyLint won't complain and using del is the solution recommended in the PyLint manual.
  • PyLint不会抱怨并且使用del是PyLint手册中推荐的解决方案。

#3


25  

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:

下划线用于我们不关心的事情,* in * args表示参数列表。因此,我们可以使用* _来表示我们不关心的事情列表:

def foo(bar, *_):
    return bar

It even passes PyCharm's checks.

它甚至通过了PyCharm的支票。

#4


2  

You can use '_' as prefix, so that pylint will ignore these parameters:

您可以使用'_'作为前缀,这样pylint将忽略这些参数:

def f(a, _b, _c):

#5


1  

If you have both args and keyword arg you should use

如果你有args和关键字arg,你应该使用

def f(a, *args, **kwargs):
    return a

#6


1  

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方式”来处理参数(我真的不知道Perl,但是在看到子语法之后我放弃了尝试学习它,手动参数解包):

Your function has 3 arguments - this is what it gets called with (Duck typing, remember?). So you get:

你的函数有3个参数 - 这是它被调用的东西(鸭子打字,还记得吗?)。所以你得到:

def funfun(a, b, c):
    return b * 2

2 unused parameters. But now, enter improved larsmans' approach:

2个未使用的参数但是现在,进入改进的larsmans的方法:

def funfun(*args):
    return args[1] * 2

And there go the warnings...

那里有警告......

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.

它保持参数名称的自记录质量。它们是一个功能,而不是一个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. 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 ;)

但是,语言本身并没有强迫你 - 你也可以反过来,让你的所有函数都有签名(* args,** kwargs),并且每次都手动解析参数。想象一下给你的控制水平。在更改“签名”(参数计数和含义)后以不推荐的方式调用时不再有异常。这是值得考虑的事情;)

#1


20  

Here's what I do with unused arguments:

这是我对未使用的参数所做的事情:

def f(a, *unused):
    return a

#2


59  

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:

这有很多好处:

  • The unused variable can still be used when calling the function both as a positional argument and as a keyword argument.
  • 在将函数作为位置参数和关键字参数调用时,仍然可以使用未使用的变量。
  • If you start to use it later, you can't since it's deleted, so there is less risk of mistakes.
  • 如果你以后开始使用它,你就不能删除,因此错误的风险会降低。
  • It's standard python syntax.
  • 这是标准的python语法。
  • PyCharm does the right thing!
  • PyCharm做对了!
  • PyLint won't complain and using del is the solution recommended in the PyLint manual.
  • PyLint不会抱怨并且使用del是PyLint手册中推荐的解决方案。

#3


25  

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:

下划线用于我们不关心的事情,* in * args表示参数列表。因此,我们可以使用* _来表示我们不关心的事情列表:

def foo(bar, *_):
    return bar

It even passes PyCharm's checks.

它甚至通过了PyCharm的支票。

#4


2  

You can use '_' as prefix, so that pylint will ignore these parameters:

您可以使用'_'作为前缀,这样pylint将忽略这些参数:

def f(a, _b, _c):

#5


1  

If you have both args and keyword arg you should use

如果你有args和关键字arg,你应该使用

def f(a, *args, **kwargs):
    return a

#6


1  

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方式”来处理参数(我真的不知道Perl,但是在看到子语法之后我放弃了尝试学习它,手动参数解包):

Your function has 3 arguments - this is what it gets called with (Duck typing, remember?). So you get:

你的函数有3个参数 - 这是它被调用的东西(鸭子打字,还记得吗?)。所以你得到:

def funfun(a, b, c):
    return b * 2

2 unused parameters. But now, enter improved larsmans' approach:

2个未使用的参数但是现在,进入改进的larsmans的方法:

def funfun(*args):
    return args[1] * 2

And there go the warnings...

那里有警告......

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.

它保持参数名称的自记录质量。它们是一个功能,而不是一个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. 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 ;)

但是,语言本身并没有强迫你 - 你也可以反过来,让你的所有函数都有签名(* args,** kwargs),并且每次都手动解析参数。想象一下给你的控制水平。在更改“签名”(参数计数和含义)后以不推荐的方式调用时不再有异常。这是值得考虑的事情;)