如何表示未使用的函数参数? [英] How can I denote unused function arguments?

查看:62
本文介绍了如何表示未使用的函数参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<预><代码>>>>a,_,_ = (1,2,3)>>>一种1

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

<预><代码>>>>def f(a,_,_): 返回一个...文件<stdin>",第 1 行语法错误:函数定义中的重复参数_"

我也试图完全省略这个论点:

<预><代码>>>>def f(a,,): 返回一个文件<stdin>",第 1 行def f(a,,): 返回一个^语法错误:无效语法

还有其他方法可以达到相同的目的吗?

解决方案

以下是我对未使用参数的处理:

def f(a, *unused):返回一个

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:

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

解决方案

Here's what I do with unused arguments:

def f(a, *unused):
    return a

这篇关于如何表示未使用的函数参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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