Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python,类中的文档字符串_Python - Fatal编程技术网

Python,类中的文档字符串

Python,类中的文档字符串,python,Python,如何在类中打印文档字符串 对于函数,我可以这样做: def func(): """ doc string """ print func.__doc__ 可以对一个类做同样的事情吗 class MyClass(object): def __init__(self): """ doc string """ i = MyClass() print i.__doc__ 这不管用。它只打印出None 我正

如何在类中打印文档字符串

对于函数,我可以这样做:

def func():  
    """
     doc string
    """


print func.__doc__
可以对一个类做同样的事情吗

class MyClass(object):

    def __init__(self):
        """
        doc string
        """

i = MyClass()  
print i.__doc__
这不管用。它只打印出
None

我正在编写一个文本游戏,我想使用doc字符串作为对玩家的指令,而不使用
print
命令


谢谢

这是
,因为类没有docstring。尝试添加一个:

class MyClass(object):
    """ Documentation for MyClass goes here. """

    def __init__(self):
        """
        doc string
        """

i = MyClass()  
print i.__doc__ # same as MyClass.__doc__

它是
None
,因为该类没有docstring。尝试添加一个:

class MyClass(object):
    """ Documentation for MyClass goes here. """

    def __init__(self):
        """
        doc string
        """

i = MyClass()  
print i.__doc__ # same as MyClass.__doc__

您为方法
MyClass定义了文档字符串。\uuuu init\uuuu
而不是
MyClass

print i.__init__.__doc__
将类的文档字符串放在类声明之后:

class MyClass(object):
    ''' MyClass ... '''
而且总是有:

help(i)

在单个文档中获取类和方法文档字符串。

您为方法
MyClass定义了一个文档字符串。\uuuu init\uuuu
而不是
MyClass

print i.__init__.__doc__
将类的文档字符串放在类声明之后:

class MyClass(object):
    ''' MyClass ... '''
而且总是有:

help(i)

在单个文档中获取类和方法文档字符串。

Ok。因此,doc字符串直接进入类中,而不是在init func中。谢谢你,好的。因此,doc字符串直接进入类中,而不是在init func中。非常感谢。