简体   繁体   English

函数定义中的星号(*)替代

[英]Alternative to Asterisk (*) in function definitions

Pylint complains when I am using an asterisk in a function call 当我在函数调用中使用星号时,Pylint抱怨

def f(*args):
    # Some code

f(1, 2, 3,)

The output given by pylint: pylint给出的输出:

pylint failed:
Used * or ** magic (star-args)

Is there an alternative to using * in function definitions, that renders the same result? 除了在函数定义中使用* ,还有其他替代方法可以呈现相同的结果吗?

I need the code to validate through pylint because it is a school assignment. 我需要通过pylint进行验证的代码,因为这是学校的作业。 I don't think it is a legit warning but I have to do as pylint says. 我认为这不是合法的警告,但我必须按照皮林特所说的做。

Pylint is highly configurable and can be rather a nuisance when it comes to certain things. Pylint具有很高的可配置性 ,在某些事情上可能会很麻烦。 Even the star-args (W0142) rule description shows it is merely a warning: 即使是star-args (W0142)规则说明也表明这仅仅是警告:

Used * or ** magic . 使用*或**魔法 Used when a function or method is called using *args or **kwargs to dispatch arguments. 当使用*args**kwargs调用函数或方法以调度参数时使用。 This doesn't improve readability and should be used with care. 这不会提高可读性,应谨慎使用。

Just disable the warning for functions where you are certain using *args is the correct thing to do, rather than try and work around PyLint: 只需对确定使用*args函数禁用警告,而不是尝试解决PyLint:

def f(*args):  # pylint: disable=star-args
    # Some code

Your only alternative is to accept one argument that you treat as a sequence, then pass in a tuple or list instead: 唯一的选择是接受一个视为序列的参数,然后传入一个元组或列表:

def f(arg):
    # Some code

f((1, 2, 3))

but I seriously doubt you'll be failed for using a # pylint disable=... comment. 但我严重怀疑您使用# pylint disable=...注释会失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM