Skip to content Skip to sidebar Skip to footer

Inconsistency In Python Help('string') Versus Help(list)?

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 only call it as a method like

Solution 1:

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 calllast):
  File "<stdin>", line 1, in<module>
NameError: name 'upper'isnot defined
>>>'hi'.upper() # methodfrom the str type
'HI'>>>from string import upper
>>>upper('hi') # functionfrom the string module'HI'

Solution 2:

You mean to do help('str'), not help('string'). str is a type, string is a module providing functions for working with strings.

Solution 3:

You are creating an instance of that object and then calling help on that instance.

So these all work:

help(1)
help({})
help([])
help('')
help(dir)
help(help)

Help grabs the docstring for that instance, and gives it back to you. When you create your own objects, you can put in useful docstrings or whatever you want.

Solution 4:

There's nothing wrong with what you see.

>>>help('string')

Will show you the string module documentation. And it looks like there's an upper function inside:

>>>import string>>>string.upper('hello')
'hello'

I'd say that this upper is the same that is called if you do:

>>>'hello'.upper()

But I'm not sure.

Notice that a string '' is a str type not a string type. This means that you're probably looking for:

>>>help('str')

And here you'll see too the str.upper method.

Solution 5:

This is because 'string' is a string. So is 'list'

To get a similar result for lists, try help([])

Post a Comment for "Inconsistency In Python Help('string') Versus Help(list)?"