Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 520273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:07:41+00:00 2026-05-13T08:07:41+00:00

Are Python docstrings and comments stored in memory when a module is loaded? I’ve

  • 0

Are Python docstrings and comments stored in memory when a module is loaded?

I’ve wondered if this is true, because I usually document my code well; may this affect memory usage?

Usually every Python object has a __doc__ method. Are those docstrings read from the file, or processed otherwise?

I’ve done searches here in the forums, Google and Mailing-Lists, but I haven’t found any relevant information.

Do you know better?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:07:42+00:00Added an answer on May 13, 2026 at 8:07 am

    By default, docstrings are present in the .pyc bytecode file, and are loaded from them (comments are not). If you use python -OO (the -OO flag stands for “optimize intensely”, as opposed to -O which stands for “optimize mildly), you get and use .pyo files instead of .pyc files, and those are optimized by omitting the docstrings (in addition to the optimizations done by -O, which remove assert statements). E.g., consider a file foo.py that has:

    """This is the documentation for my module foo."""
    
    def bar(x):
      """This is the documentation for my function foo.bar."""
      return x + 1
    

    you could have the following shell session…:

    $ python -c'import foo; print foo.bar(22); print foo.__doc__'
    23
    This is the documentation for my module foo.
    $ ls -l foo.pyc
    -rw-r--r--  1 aleax  eng  327 Dec 30 16:17 foo.pyc
    $ python -O -c'import foo; print foo.bar(22); print foo.__doc__'
    23
    This is the documentation for my module foo.
    $ ls -l foo.pyo
    -rw-r--r--  1 aleax  eng  327 Dec 30 16:17 foo.pyo
    $ python -OO -c'import foo; print foo.bar(22); print foo.__doc__'
    23
    This is the documentation for my module foo.
    $ ls -l foo.pyo
    -rw-r--r--  1 aleax  eng  327 Dec 30 16:17 foo.pyo
    $ rm foo.pyo
    $ python -OO -c'import foo; print foo.bar(22); print foo.__doc__'
    23
    None
    $ ls -l foo.pyo
    -rw-r--r--  1 aleax  eng  204 Dec 30 16:17 foo.pyo
    

    Note that, since we used -O first, the .pyo file was 327 bytes — even after using -OO, because the .pyo file was still around and Python didn’t rebuild/overwrite it, it just used the existing one. Removing the existing .pyo (or, equivalently, touch foo.py so that Python knows the .pyo is “out of date”) means that Python rebuilds it (and, in this case, saves 123 bytes on disk, and a little bit more when the module’s imported — but all .__doc__ entries disappear and are replaced by None).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 462k
  • Answers 462k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You usually do it when building the shared library. There… May 16, 2026 at 12:31 am
  • Editorial Team
    Editorial Team added an answer Non-specified context defaults to document. By default, selectors perform their… May 16, 2026 at 12:31 am
  • Editorial Team
    Editorial Team added an answer Jex, A quick google search for "clipping mask in openGL"… May 16, 2026 at 12:31 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.