What is the meaning of a forward slash “/” in a Python method signature, as shown by help(foo)? [duplicate]

两盒软妹~` 提交于 2019-11-26 09:45:50

问题


This question already has an answer here:

  • Python: What does the slash mean in help() output? 2 answers

In the signature returned interactively by help(foo), what is the meaning of a /?

In [37]: help(object.__eq__)

Help on wrapper_descriptor:

__eq__(self, value, /)
    Return self==value.

In [55]: help(object.__init__)

Help on wrapper_descriptor:

__init__(self, /, *args, **kwargs)
    Initialize self.  See help(type(self)) for accurate signature.

I thought it might be related to keyword-only arguments, but it\'s not. When I create my own function with keyword-only arguments, positional and keyword-only arguments are separated by * (as expected), not by /. What does the / mean?


回答1:


As explained here, the '/' as a parameter marks the end of parameters that are positional only (see here), i.e. parameters you can't use as keyword parameters. In the case of __eq__(self, value, /) the slash is at the end, which means that all parameters are marked as positional only while in the case of your __init__ only self is positional only.



来源:https://stackoverflow.com/questions/28243832/what-is-the-meaning-of-a-forward-slash-in-a-python-method-signature-as-show

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!