Python模块docstring

2024-05-18 02:28:57 发布

您现在位置:Python中文网/ 问答频道 /正文

如何从主文件打印python模块文件的docstring?假设我有一个名为spolder.py的python模块文件,其中定义了一个名为spolder的类。然后我有另一个python文件main.py。在main中,我想访问模块级docstring。我该怎么做?
我知道如何打印类和方法的docstring。 例如,要从我的主文件打印类级别的docstring,它将是:

print(Projectile.__doc__)

Output:  This is the class docstring:  Simulates the flight of simple projectiles near the earth's surface, ignoring wind resistance. Tracking is done in two dimensions, height (y) and distance (x).

但是如何打印模块级docstring??谢谢

     """This is the module docstring"""
        from graphics import *
        from math import *

        class Projectile:
            """This is the class docstring:  Simulates the flight of simple projectiles near the earth's surface, 
               ignoring wind resistance. Tracking is done in two dimensions, height (y) and distance (x)."""

            # Constructor - TO INITIALIZE INSTANCE VARIABLES
            """This is the constructor docstring"""
            def __init__(self, angle, velocity, height):
                self.initialVelocity = velocity
                self.xpos = 0.0

Tags: 模块文件thepyselfismainthis
1条回答
网友
1楼 · 发布于 2024-05-18 02:28:57

您可以使用__doc__字段获取docstring:

import x; print(x.__doc__)

或使用help函数获取交互式帮助:

import x; help(x)

相关问题 更多 >