Inconsistency in python help('string') versus help(list)?

后端 未结 5 747
旧时难觅i
旧时难觅i 2021-01-25 05:58

When I type help(\'string\') in the python interpreter I get information about the string class. There,upper() is indicated as a function. Yet I can on

5条回答
  •  借酒劲吻你
    2021-01-25 06:39

    When you searched for help('string'), you were looking for the docstrings of the string module. If you do help(str) or help('str') you'll get the docstrings of the str type, and here upper appears as a method!

    As you can see here, the function upper from the string module is actually a function and not a method:

    >>> upper('hi')
    Traceback (most recent call last):
      File "", line 1, in 
    NameError: name 'upper' is not defined
    >>> 'hi'.upper() # method from the str type
    'HI'
    >>> from string import upper
    >>> upper('hi') # function from the string module
    'HI'
    

提交回复
热议问题