How to selectively override the text from python's help(MYclass) with something shorter and customized?
I'm learning to write user-friendly classes and methods and I'd like for users to know how to use them from a terminal. Python's standard help(MYclass) returns eighteen lines I don't want, and that's half a screen for a small window and those just learning python will lose continuity with the previous lines disappearing from the top.
Is there any way I can override what using help(MYclass) (or help(MYmethod)) shows so that it only displays the (in this case) one-line docstring?
While some IDEs show the docstrings in bubbles, terminals don't cooperate. (Do the triple-quoted (docstring) messages in Python appear while typing in IDEs other than IDLE?):

So instead, I've turned to help for help, but help is overly helpful with all those extra lines of template.
Then I thought of redefining help:
def hhelp(x):
return x.__doc__
help = hhelp
but I decided that this is evil; like redefining the number 7, and I would like help(some builtin) to still work normally, and the selective hijacking to occur only for MYclasses.
There is always
def doc(x):
return x.__doc__
if I can't find anything that selectively hijacks help().
class A(object):
"""instantiate a = A(x), then use a.sqr() to get x**2"""
def __init__(self, x):
self.x = x
def sqr(self):
return x**2
results in nineteen lines. I'd only like my one-line docstring to show.
Help on class A in module __main__:
class A(__builtin__.object)
| instantiate a = A(x), then use a.sqr() to get x**2
|
| Methods defined here:
|
| __init__(self, x)
|
| sqr(self)
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
python python-2.7 docstring
add a comment |
I'm learning to write user-friendly classes and methods and I'd like for users to know how to use them from a terminal. Python's standard help(MYclass) returns eighteen lines I don't want, and that's half a screen for a small window and those just learning python will lose continuity with the previous lines disappearing from the top.
Is there any way I can override what using help(MYclass) (or help(MYmethod)) shows so that it only displays the (in this case) one-line docstring?
While some IDEs show the docstrings in bubbles, terminals don't cooperate. (Do the triple-quoted (docstring) messages in Python appear while typing in IDEs other than IDLE?):

So instead, I've turned to help for help, but help is overly helpful with all those extra lines of template.
Then I thought of redefining help:
def hhelp(x):
return x.__doc__
help = hhelp
but I decided that this is evil; like redefining the number 7, and I would like help(some builtin) to still work normally, and the selective hijacking to occur only for MYclasses.
There is always
def doc(x):
return x.__doc__
if I can't find anything that selectively hijacks help().
class A(object):
"""instantiate a = A(x), then use a.sqr() to get x**2"""
def __init__(self, x):
self.x = x
def sqr(self):
return x**2
results in nineteen lines. I'd only like my one-line docstring to show.
Help on class A in module __main__:
class A(__builtin__.object)
| instantiate a = A(x), then use a.sqr() to get x**2
|
| Methods defined here:
|
| __init__(self, x)
|
| sqr(self)
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
python python-2.7 docstring
add a comment |
I'm learning to write user-friendly classes and methods and I'd like for users to know how to use them from a terminal. Python's standard help(MYclass) returns eighteen lines I don't want, and that's half a screen for a small window and those just learning python will lose continuity with the previous lines disappearing from the top.
Is there any way I can override what using help(MYclass) (or help(MYmethod)) shows so that it only displays the (in this case) one-line docstring?
While some IDEs show the docstrings in bubbles, terminals don't cooperate. (Do the triple-quoted (docstring) messages in Python appear while typing in IDEs other than IDLE?):

So instead, I've turned to help for help, but help is overly helpful with all those extra lines of template.
Then I thought of redefining help:
def hhelp(x):
return x.__doc__
help = hhelp
but I decided that this is evil; like redefining the number 7, and I would like help(some builtin) to still work normally, and the selective hijacking to occur only for MYclasses.
There is always
def doc(x):
return x.__doc__
if I can't find anything that selectively hijacks help().
class A(object):
"""instantiate a = A(x), then use a.sqr() to get x**2"""
def __init__(self, x):
self.x = x
def sqr(self):
return x**2
results in nineteen lines. I'd only like my one-line docstring to show.
Help on class A in module __main__:
class A(__builtin__.object)
| instantiate a = A(x), then use a.sqr() to get x**2
|
| Methods defined here:
|
| __init__(self, x)
|
| sqr(self)
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
python python-2.7 docstring
I'm learning to write user-friendly classes and methods and I'd like for users to know how to use them from a terminal. Python's standard help(MYclass) returns eighteen lines I don't want, and that's half a screen for a small window and those just learning python will lose continuity with the previous lines disappearing from the top.
Is there any way I can override what using help(MYclass) (or help(MYmethod)) shows so that it only displays the (in this case) one-line docstring?
While some IDEs show the docstrings in bubbles, terminals don't cooperate. (Do the triple-quoted (docstring) messages in Python appear while typing in IDEs other than IDLE?):

So instead, I've turned to help for help, but help is overly helpful with all those extra lines of template.
Then I thought of redefining help:
def hhelp(x):
return x.__doc__
help = hhelp
but I decided that this is evil; like redefining the number 7, and I would like help(some builtin) to still work normally, and the selective hijacking to occur only for MYclasses.
There is always
def doc(x):
return x.__doc__
if I can't find anything that selectively hijacks help().
class A(object):
"""instantiate a = A(x), then use a.sqr() to get x**2"""
def __init__(self, x):
self.x = x
def sqr(self):
return x**2
results in nineteen lines. I'd only like my one-line docstring to show.
Help on class A in module __main__:
class A(__builtin__.object)
| instantiate a = A(x), then use a.sqr() to get x**2
|
| Methods defined here:
|
| __init__(self, x)
|
| sqr(self)
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
python python-2.7 docstring
python python-2.7 docstring
edited Jan 1 at 15:25
Tom Zych
10.6k82846
10.6k82846
asked Jan 1 at 13:45
uhohuhoh
1,4701939
1,4701939
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You can monkeypatch the built-in help function to show the __doc__ string for classes and to fall back on the original help for other objects:
import inspect
import pydoc
class Helper(object):
def __init__(self):
self.help = __builtins__.help
def __call__(self, obj):
if inspect.isclass(obj):
return pydoc.pager(obj.__doc__)
else:
return self.help(obj)
__builtins__.help = Helper()
Which produces the following output:
class Foo:
"""This is the docstring."""
def foo(self):
return None
help(Foo)
# Output:
# This is the docstring.
This is exactly what I need, thank you! If I take some time to stop and smell the bananas I could probably come up with some way to "refine the monkey" to handle my classes but leave things likelistalone.
– uhoh
Jan 2 at 2:48
1
@uhoh You could useinspect.isclass(obj) and not any(map(lambda x: x is obj, (list, tuple, set, dict, ...)))or if you have all your classes inherit from some custom base classMyBaseyou could useissubclass(obj, MyBase). To be more complete on the above enumeration you could also usemap(lambda x: getattr(__builtins__, x), dir(__builtins__)to obtain a full list of built-in objects.
– a_guest
Jan 2 at 10:41
I was in the middle of formulating a new question on just that, I'll plug those in to my unfinished abstracted script to see if you have pre-answered it. I'll flag you here if I decide to go ahead with the post. Thank you again!
– uhoh
Jan 2 at 11:24
add a comment |
As help(help) indicates it relies on pydoc, specifically pydoc.help.
| Define the builtin 'help'.
| This is a wrapper around pydoc.help (with a twist).
Inspecting the source code we find that it relies on TextDoc.docclass to generate the help text for classes. So we can monkeypatch that method in order to generate our own help text:1
import pydoc
def docclass(self, object, *args, **kwargs):
return pydoc.getdoc(object)
pydoc.TextDoc.docclass = docclass
Now we'll only get the __doc__ string in the help text:
class Foo:
"""This is the docstring."""
def foo(self):
return None
help(Foo)
# Output:
# Help on class Foo in module __main__:
#
# This is the docstring.
1. The syntax is specific to Python 2.7 since the OP uses the corresponding tag. Though it seems to work with other versions of Python as well.
Thanks also for the explanation. I learned some useful things today, yay!
– uhoh
Jan 2 at 2:49
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995957%2fhow-to-selectively-override-the-text-from-pythons-helpmyclass-with-something%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can monkeypatch the built-in help function to show the __doc__ string for classes and to fall back on the original help for other objects:
import inspect
import pydoc
class Helper(object):
def __init__(self):
self.help = __builtins__.help
def __call__(self, obj):
if inspect.isclass(obj):
return pydoc.pager(obj.__doc__)
else:
return self.help(obj)
__builtins__.help = Helper()
Which produces the following output:
class Foo:
"""This is the docstring."""
def foo(self):
return None
help(Foo)
# Output:
# This is the docstring.
This is exactly what I need, thank you! If I take some time to stop and smell the bananas I could probably come up with some way to "refine the monkey" to handle my classes but leave things likelistalone.
– uhoh
Jan 2 at 2:48
1
@uhoh You could useinspect.isclass(obj) and not any(map(lambda x: x is obj, (list, tuple, set, dict, ...)))or if you have all your classes inherit from some custom base classMyBaseyou could useissubclass(obj, MyBase). To be more complete on the above enumeration you could also usemap(lambda x: getattr(__builtins__, x), dir(__builtins__)to obtain a full list of built-in objects.
– a_guest
Jan 2 at 10:41
I was in the middle of formulating a new question on just that, I'll plug those in to my unfinished abstracted script to see if you have pre-answered it. I'll flag you here if I decide to go ahead with the post. Thank you again!
– uhoh
Jan 2 at 11:24
add a comment |
You can monkeypatch the built-in help function to show the __doc__ string for classes and to fall back on the original help for other objects:
import inspect
import pydoc
class Helper(object):
def __init__(self):
self.help = __builtins__.help
def __call__(self, obj):
if inspect.isclass(obj):
return pydoc.pager(obj.__doc__)
else:
return self.help(obj)
__builtins__.help = Helper()
Which produces the following output:
class Foo:
"""This is the docstring."""
def foo(self):
return None
help(Foo)
# Output:
# This is the docstring.
This is exactly what I need, thank you! If I take some time to stop and smell the bananas I could probably come up with some way to "refine the monkey" to handle my classes but leave things likelistalone.
– uhoh
Jan 2 at 2:48
1
@uhoh You could useinspect.isclass(obj) and not any(map(lambda x: x is obj, (list, tuple, set, dict, ...)))or if you have all your classes inherit from some custom base classMyBaseyou could useissubclass(obj, MyBase). To be more complete on the above enumeration you could also usemap(lambda x: getattr(__builtins__, x), dir(__builtins__)to obtain a full list of built-in objects.
– a_guest
Jan 2 at 10:41
I was in the middle of formulating a new question on just that, I'll plug those in to my unfinished abstracted script to see if you have pre-answered it. I'll flag you here if I decide to go ahead with the post. Thank you again!
– uhoh
Jan 2 at 11:24
add a comment |
You can monkeypatch the built-in help function to show the __doc__ string for classes and to fall back on the original help for other objects:
import inspect
import pydoc
class Helper(object):
def __init__(self):
self.help = __builtins__.help
def __call__(self, obj):
if inspect.isclass(obj):
return pydoc.pager(obj.__doc__)
else:
return self.help(obj)
__builtins__.help = Helper()
Which produces the following output:
class Foo:
"""This is the docstring."""
def foo(self):
return None
help(Foo)
# Output:
# This is the docstring.
You can monkeypatch the built-in help function to show the __doc__ string for classes and to fall back on the original help for other objects:
import inspect
import pydoc
class Helper(object):
def __init__(self):
self.help = __builtins__.help
def __call__(self, obj):
if inspect.isclass(obj):
return pydoc.pager(obj.__doc__)
else:
return self.help(obj)
__builtins__.help = Helper()
Which produces the following output:
class Foo:
"""This is the docstring."""
def foo(self):
return None
help(Foo)
# Output:
# This is the docstring.
answered Jan 1 at 18:16
a_guesta_guest
6,77831243
6,77831243
This is exactly what I need, thank you! If I take some time to stop and smell the bananas I could probably come up with some way to "refine the monkey" to handle my classes but leave things likelistalone.
– uhoh
Jan 2 at 2:48
1
@uhoh You could useinspect.isclass(obj) and not any(map(lambda x: x is obj, (list, tuple, set, dict, ...)))or if you have all your classes inherit from some custom base classMyBaseyou could useissubclass(obj, MyBase). To be more complete on the above enumeration you could also usemap(lambda x: getattr(__builtins__, x), dir(__builtins__)to obtain a full list of built-in objects.
– a_guest
Jan 2 at 10:41
I was in the middle of formulating a new question on just that, I'll plug those in to my unfinished abstracted script to see if you have pre-answered it. I'll flag you here if I decide to go ahead with the post. Thank you again!
– uhoh
Jan 2 at 11:24
add a comment |
This is exactly what I need, thank you! If I take some time to stop and smell the bananas I could probably come up with some way to "refine the monkey" to handle my classes but leave things likelistalone.
– uhoh
Jan 2 at 2:48
1
@uhoh You could useinspect.isclass(obj) and not any(map(lambda x: x is obj, (list, tuple, set, dict, ...)))or if you have all your classes inherit from some custom base classMyBaseyou could useissubclass(obj, MyBase). To be more complete on the above enumeration you could also usemap(lambda x: getattr(__builtins__, x), dir(__builtins__)to obtain a full list of built-in objects.
– a_guest
Jan 2 at 10:41
I was in the middle of formulating a new question on just that, I'll plug those in to my unfinished abstracted script to see if you have pre-answered it. I'll flag you here if I decide to go ahead with the post. Thank you again!
– uhoh
Jan 2 at 11:24
This is exactly what I need, thank you! If I take some time to stop and smell the bananas I could probably come up with some way to "refine the monkey" to handle my classes but leave things like
list alone.– uhoh
Jan 2 at 2:48
This is exactly what I need, thank you! If I take some time to stop and smell the bananas I could probably come up with some way to "refine the monkey" to handle my classes but leave things like
list alone.– uhoh
Jan 2 at 2:48
1
1
@uhoh You could use
inspect.isclass(obj) and not any(map(lambda x: x is obj, (list, tuple, set, dict, ...))) or if you have all your classes inherit from some custom base class MyBase you could use issubclass(obj, MyBase). To be more complete on the above enumeration you could also use map(lambda x: getattr(__builtins__, x), dir(__builtins__) to obtain a full list of built-in objects.– a_guest
Jan 2 at 10:41
@uhoh You could use
inspect.isclass(obj) and not any(map(lambda x: x is obj, (list, tuple, set, dict, ...))) or if you have all your classes inherit from some custom base class MyBase you could use issubclass(obj, MyBase). To be more complete on the above enumeration you could also use map(lambda x: getattr(__builtins__, x), dir(__builtins__) to obtain a full list of built-in objects.– a_guest
Jan 2 at 10:41
I was in the middle of formulating a new question on just that, I'll plug those in to my unfinished abstracted script to see if you have pre-answered it. I'll flag you here if I decide to go ahead with the post. Thank you again!
– uhoh
Jan 2 at 11:24
I was in the middle of formulating a new question on just that, I'll plug those in to my unfinished abstracted script to see if you have pre-answered it. I'll flag you here if I decide to go ahead with the post. Thank you again!
– uhoh
Jan 2 at 11:24
add a comment |
As help(help) indicates it relies on pydoc, specifically pydoc.help.
| Define the builtin 'help'.
| This is a wrapper around pydoc.help (with a twist).
Inspecting the source code we find that it relies on TextDoc.docclass to generate the help text for classes. So we can monkeypatch that method in order to generate our own help text:1
import pydoc
def docclass(self, object, *args, **kwargs):
return pydoc.getdoc(object)
pydoc.TextDoc.docclass = docclass
Now we'll only get the __doc__ string in the help text:
class Foo:
"""This is the docstring."""
def foo(self):
return None
help(Foo)
# Output:
# Help on class Foo in module __main__:
#
# This is the docstring.
1. The syntax is specific to Python 2.7 since the OP uses the corresponding tag. Though it seems to work with other versions of Python as well.
Thanks also for the explanation. I learned some useful things today, yay!
– uhoh
Jan 2 at 2:49
add a comment |
As help(help) indicates it relies on pydoc, specifically pydoc.help.
| Define the builtin 'help'.
| This is a wrapper around pydoc.help (with a twist).
Inspecting the source code we find that it relies on TextDoc.docclass to generate the help text for classes. So we can monkeypatch that method in order to generate our own help text:1
import pydoc
def docclass(self, object, *args, **kwargs):
return pydoc.getdoc(object)
pydoc.TextDoc.docclass = docclass
Now we'll only get the __doc__ string in the help text:
class Foo:
"""This is the docstring."""
def foo(self):
return None
help(Foo)
# Output:
# Help on class Foo in module __main__:
#
# This is the docstring.
1. The syntax is specific to Python 2.7 since the OP uses the corresponding tag. Though it seems to work with other versions of Python as well.
Thanks also for the explanation. I learned some useful things today, yay!
– uhoh
Jan 2 at 2:49
add a comment |
As help(help) indicates it relies on pydoc, specifically pydoc.help.
| Define the builtin 'help'.
| This is a wrapper around pydoc.help (with a twist).
Inspecting the source code we find that it relies on TextDoc.docclass to generate the help text for classes. So we can monkeypatch that method in order to generate our own help text:1
import pydoc
def docclass(self, object, *args, **kwargs):
return pydoc.getdoc(object)
pydoc.TextDoc.docclass = docclass
Now we'll only get the __doc__ string in the help text:
class Foo:
"""This is the docstring."""
def foo(self):
return None
help(Foo)
# Output:
# Help on class Foo in module __main__:
#
# This is the docstring.
1. The syntax is specific to Python 2.7 since the OP uses the corresponding tag. Though it seems to work with other versions of Python as well.
As help(help) indicates it relies on pydoc, specifically pydoc.help.
| Define the builtin 'help'.
| This is a wrapper around pydoc.help (with a twist).
Inspecting the source code we find that it relies on TextDoc.docclass to generate the help text for classes. So we can monkeypatch that method in order to generate our own help text:1
import pydoc
def docclass(self, object, *args, **kwargs):
return pydoc.getdoc(object)
pydoc.TextDoc.docclass = docclass
Now we'll only get the __doc__ string in the help text:
class Foo:
"""This is the docstring."""
def foo(self):
return None
help(Foo)
# Output:
# Help on class Foo in module __main__:
#
# This is the docstring.
1. The syntax is specific to Python 2.7 since the OP uses the corresponding tag. Though it seems to work with other versions of Python as well.
edited Jan 1 at 18:14
answered Jan 1 at 18:08
a_guesta_guest
6,77831243
6,77831243
Thanks also for the explanation. I learned some useful things today, yay!
– uhoh
Jan 2 at 2:49
add a comment |
Thanks also for the explanation. I learned some useful things today, yay!
– uhoh
Jan 2 at 2:49
Thanks also for the explanation. I learned some useful things today, yay!
– uhoh
Jan 2 at 2:49
Thanks also for the explanation. I learned some useful things today, yay!
– uhoh
Jan 2 at 2:49
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995957%2fhow-to-selectively-override-the-text-from-pythons-helpmyclass-with-something%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
