【问题标题】:What does a single * without identifier mean in the Python3 functions? [duplicate]Python3 函数中没有标识符的单个 * 是什么意思? [复制]
【发布时间】:2012-07-07 03:09:04
【问题描述】:

Python 函数参数中没有标识符的单个 * 是什么意思? 这是一个适用于 Python3.2 的示例:

class Shape:
    def __init__(self, *, shapename, **kwds):
        self.shapename = shapename
        super().__init__(**kwds)

对我来说,自我之后的星星很奇怪。

我在这里找到了它(从第 46 行开始): http://code.activestate.com/recipes/577720-how-to-use-super-effectively/

【问题讨论】:

  • 为指向 PEP 的人加分 ;) (我记得读过一段 PEP 描述这一点,但我不记得在哪里,或者它是什么,/me 去了返回谷歌

标签: python function python-3.x


【解决方案1】:

唯一的* 表示后面的所有参数都是关键字参数,也就是说,它们只能使用它们的名称提供,而不是作为位置参数。

更多详情请参阅PEP 3102

【讨论】:

  • 换句话说:shapename 必须在创建新的Shape 对象时显式添加,例如Shape(shapename='Circle')
猜你喜欢
  • 1970-01-01
  • 2011-07-08
  • 1970-01-01
  • 1970-01-01
  • 2015-02-13
  • 1970-01-01
  • 1970-01-01
  • 2017-04-11
  • 2011-06-15
相关资源
最近更新 更多