I'm trying to understand how to get useful results when the help function is used to interrogate objects created in my code. I'm puzzled by different behavior for different classes.
Cls1 = type( 'FirstClass', (str,), {'__doc__':'My new class'})
inst1 = Cls1('Hello World')
Cls2 = type( 'SecondClass', (object,), {'__doc__':'My second new class'})
inst2 = Cls2( )
help(inst1)
yields No Python documentation found for 'Hello World'
, while help(inst2)
yields:
Help on SecondClass in module __main__ object:
class SecondClass(builtins.object)
| My second new class
|
...
I would like to create a class based on str
and be able to have useful messages displayed by the help
function: is there a simple way of achieving this?
If you want to create a subclass of
str
and show the hints withhelp
built-in, you can use docstrings. For instance the following subclasshas the following output on
help(NewString)
But as for all instances of string the
help
method will not be useful.The reason it fails is that when passing a
str
tohelp
build-in it is treated as the name of a function, and as there obviously is not a function namedHello World
it shows an error.Running the following
help('help')
will output:which is the help on
help
.