简体   繁体   English

在实践中,Python args和kwargs是否有其他名称?

[英]Are Python args and kwargs ever named something else in practice?

Python does not restrict the names of parameters, however some parameter names are strongly governed by convention, such as self , cls , args , and kwargs . Python并不限制参数的名称,但是某些参数的名称受约定严格控制,例如selfclsargskwargs The names self and cls always represent the same concept, so in those cases it's hard for me to see a compelling reason for someone to veer from convention. selfcls这两个名称始终代表相同的概念,因此在那种情况下,我很难看出有人有理由偏离常规的令人信服的理由。 With args and kwargs , however, I find this naming convention stifling. 但是,对于argskwargs ,我发现这种命名约定令人窒息。

Say I have a class that has various properties that can be set via passing kwargs to its constructor: 假设我有一个具有各种属性的类,可以通过将kwargs传递给其构造函数来进行设置:

class MyObj:
    def __init__(self, **kwargs):
        for propname in kwargs:
            self.set_property(propname, kwargs[propname])

In this case, the kwargs are only intended to be settable properties of an instance of that class, so to me it would make sense to write the definition as follows: 在这种情况下,仅将kwargs用作该类实例的可设置属性,因此对我而言,如下定义定义是有意义的:

class MyObj:
    def __init__(self, **properties):
        for propname in properties:
            self.set_property(propname, properties[propname])

That way, one only need look at the method's signature to get an idea of what the kwargs are for. 这样,只需查看该方法的签名即可了解kwarg的用途。

Generally, I believe convention is a good thing. 总的来说,我相信惯例是一件好事。 However, it seems to me that always using args and kwargs is a missed opportunity to convey useful information to the user of an API about the nature of a function/method's args and kwargs. 但是,在我看来,始终使用argskwargs是一个错失良机,无法向API用户传达有关函数/方法的args和kwargs性质的有用信息。 After all, the fact that they are unnamed or named arguments is already made clear by the presence of the single or double asterisk. 毕竟,单星号或双星号已经清楚地表明了它们未命名或命名的事实。

Does anyone have examples of alternate names for args and kwargs being used in real-world, multi-developer code or is it just too against the grain to use other variable names for these constructs? 有人在现实世界,多开发人员代码中使用过args和kwargs的替代名称的示例吗,还是对这些结构使用其他变量名称太过于严格了?

If it really is just a horrible, horrible idea to not use these conventional names, what is the reason for that? 如果真的不使用这些常规名称只是一个可怕的想法,那是什么原因呢?

I don't see any reason against using context specific names, such as ... 我看不出有任何理由反对使用特定于上下文的名称,例如...

my_sum(*numbers) , my_parser(*strings, **flags) , etc. my_sum(*numbers)my_parser(*strings, **flags)

The print docs say *objects . print文档说*objects

The zip docs say *iterables . zip文档说*iterables

itertools.chain uses *iterables in the docs and in the docstring. itertools.chain在文档和文档字符串中使用*iterables

>>> from itertools import chain
>>> chain.__doc__.splitlines()[0]
'chain(*iterables) --> chain object'

collections.ChainMap uses *maps in the docs and in __init__ . collections.ChainMap在文档和__init__使用*maps

>>> from collections import ChainMap
>>> from inspect import signature
>>> signature(ChainMap.__init__)
<Signature (self, *maps)>

Feel free to hunt for more examples. 随意寻找更多示例。

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

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