How do I print documentation strings within classes
For functions i can do this:
def func():
"""
doc string
"""
print func.__doc__
Is it possible to do the same thing with a class
class MyClass(object):
def __init__(self):
"""
doc string
"""
i = MyClass()
print i.__doc__
This doesn't work. It only prints out None
.
print
command
Thanks
It's None
because the class does not have a docstring. Try adding one:
class MyClass(object):
""" Documentation for MyClass goes here. """
def __init__(self):
"""
doc string
"""
i = MyClass()
print i.__doc__ # same as MyClass.__doc__
You defined a doc string for the method MyClass.__init__
not MyClass
:
print i.__init__.__doc__
Put the doc string for the class after the class declaration:
class MyClass(object):
''' MyClass ... '''
And there is always:
help(i)
To get the class and method doc strings in a single document.
精彩评论