What does the slash mean in help() output?

前端 未结 3 1967
失恋的感觉
失恋的感觉 2020-11-22 04:02

What does the / mean in Python 3.4\'s help output for range before the closing parenthesis?

>>> help(range)
H         


        
3条回答
  •  故里飘歌
    2020-11-22 04:59

    I asked this question myself. :) Found out that / was originally proposed by Guido in here.

    Alternative proposal: how about using '/' ? It's kind of the opposite of '*' which means "keyword argument", and '/' is not a new character.

    Then his proposal won.

    Heh. If that's true, my '/' proposal wins:

     def foo(pos_only, /, pos_or_kw, *, kw_only): ...
    

    I think the very relevant document covering this is PEP 570. Where recap section looks nice.

    Recap

    The use case will determine which parameters to use in the function definition:

     def f(pos1, pos2, /, pos_or_kwd, *, kwd1, kwd2):
    

    As guidance:

    Use positional-only if names do not matter or have no meaning, and there are only a few arguments which will always be passed in the same order. Use keyword-only when names have meaning and the function definition is more understandable by being explicit with names.


    If the function ends with /

    def foo(p1, p2, /)
    

    This means all functional arguments are positional.

提交回复
热议问题