开发者

Python, documentation strings within classes

开发者 https://www.devze.com 2023-02-06 02:23 出处:网络
How do I print documentation strings within classes For functions i can do this: def func(): \"\"\" doc string

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.

Am writing a text game and I want to use the doc string as an instruction to the playe开发者_StackOverflow社区r without using the 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消