3

How do I print the docstring of my python module file, from my main file? Let's say I have a python module file called Projectile.py, which defines a class inside it called Projectile. Then I have another python file called main.py. In main, I want to access the module level docstring. How do I do this?
I know how to print the docstring for the class and the methods. For example, to print the class level docstring from my main file, it would just be:

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).

But how to print the module level docstring?? Thanks!

     """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
0

1 Answer 1

2

You can get the docstring, using __doc__ field:

import x; print(x.__doc__)

Or use help function to get interactive help:

import x; help(x)
Sign up to request clarification or add additional context in comments.

5 Comments

This prints out all the info on my Projectile.py file, but doesn't print the docstring at the very top of the file/module. In my example, from my main.py file, I'm trying to print out the docstring """This is the module docstring""".
Got it. I extended my answer.
when I do print(Projectile.__doc__), it prints my class docstring, but not the module docstring. Is this because I'm naming my module the same as my class?
How do you import Projectile class? Don't use from Projectile import Projectile.
Duh! OMG that was it! Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.