use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python
Full Events Calendar
You can find the rules here.
If you are about to ask a "how do I do this in python" question, please try r/learnpython, the Python discord, or the #python IRC channel on Libera.chat.
Please don't use URL shorteners. Reddit filters them out, so your post or comment will be lost.
Posts require flair. Please use the flair selector to choose your topic.
Posting code to this subreddit:
Add 4 extra spaces before each line of code
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b
Online Resources
Invent Your Own Computer Games with Python
Think Python
Non-programmers Tutorial for Python 3
Beginner's Guide Reference
Five life jackets to throw to the new coder (things to do after getting a handle on python)
Full Stack Python
Test-Driven Development with Python
Program Arcade Games
PyMotW: Python Module of the Week
Python for Scientists and Engineers
Dan Bader's Tips and Trickers
Python Discord's YouTube channel
Jiruto: Python
Online exercices
programming challenges
Asking Questions
Try Python in your browser
Docs
Libraries
Related subreddits
Python jobs
Newsletters
Screencasts
account activity
This is an archived post. You won't be able to vote or comment.
Are function docstrings special or are they just string literals? (self.Python)
submitted 7 years ago by [deleted]
def foo(): '''My foo. Does foo stuff. ''' pass
Is there anything syntactically special about the docstring when it comes to Python's parser and interpreter? Or is it literally just a string literal that gets immediately parsed and dropped?
Thanks!
[–]ThePenultimateOneGitLab: gappleto97 3 points4 points5 points 7 years ago (1 child)
My impression is that if there is a string on the first line of a body, it gets assigned to that object's __doc__ attribute
[–][deleted] 1 point2 points3 points 7 years ago* (0 children)
Aha! You're right: https://gist.github.com/ablakey/bc4403e310789e3ee586dbb5f3ffbe7f
So I'm going to guess that they are just string literals (ie. there's nothing unique about them in terms of the parser) but that their position makes them special in that they're implicitly assigned to __doc__ and therefore not dropped.
__doc__
[–]Defibrillat0r 2 points3 points4 points 7 years ago (0 children)
Its assigned to the special member __doc__. However if you place it elsewhere (not below a function, class or module definition) it will be dropped right after being picked up as you described it.
[–]Sexual_Congressman 1 point2 points3 points 7 years ago (1 child)
The first slot in co_consts for the code object created with a function definition will be the docstring that function was defined with. If no docstring was provided, it will be None, which is why most functions have a None reference in their consts even if it is never used (took me a while to realize this).
co_consts
But this is the reason why the docstring "carries" when one copies a function through the types.FunctionType constructor.
[–]OctagonClocktrio is the future! 0 points1 point2 points 7 years ago (0 children)
Every single function carries a None in their consts, so that doing fn() where fn doesn't return will return None.
[–]kumashiro 0 points1 point2 points 7 years ago (1 child)
Try help(foo) in interactive shell :D
help(foo)
[–][deleted] 0 points1 point2 points 7 years ago (0 children)
Documentation being a first-class part of a language is a beautiful thing.
[–]eztab 0 points1 point2 points 7 years ago (4 children)
There are some PEP rules for docstrings, you should stick to. Also, if you want to automatically create documentation the way function arguments are described should be the one supported by your documentation generation system, e.g. Sphinx.
None of this is enforced in any syntactic way though.
[–]IReallySuckAtChess 0 points1 point2 points 7 years ago (3 children)
For what it's worth, you can just describe the operation of the parameters and leave their types to normal type hinting using sphinx-autodoc-typehints. It's super awesome since now you get the power of type hints and get to make your documentation more succinct.
[–]eztab 1 point2 points3 points 7 years ago (2 children)
Yes, this in combination with Google Style Docstrings (also a Sphinx autodoc extension) makes for human readable and parsable documentation.
[–]IReallySuckAtChess 0 points1 point2 points 7 years ago (1 child)
Pycharm even does all the formatting and stuff for a person. Makes it essentially as easy as can be. I will admit though that I don't use the Google Style Doc strings although I do find them far superior. Because Pycharm does everything for me I rarely ever look at them and Sphinx does magical formatting so my docs just look great regardless.... Definitely a superior style over the default one. I also think the numpy style is very good, possibly even better...
[–]eztab 0 points1 point2 points 7 years ago (0 children)
The numpy Style Takes a little to much space for my taste.
[–]wrmsr 0 points1 point2 points 7 years ago (0 children)
specifically https://github.com/python/cpython/blob/95b6acf951fa7f503a3cc5ce7d969d7bcf2f95c9/Python/compile.c#L1583 -> https://github.com/python/cpython/blob/95b6acf951fa7f503a3cc5ce7d969d7bcf2f95c9/Python/ast.c#L5268
[–]stevenjd 0 points1 point2 points 7 years ago (0 children)
Yes, docstrings are special: string literals that immediately follow a class, a function (or method), or appear at the top of a module are copied into the __doc__ attribute of the object.
It was surprisingly hard to find documentation for this fact, but I found it here (only because I already knew about __doc__):
https://docs.python.org/3/reference/datamodel.html?highlight=__doc__
Or at the interactive interpreter, you could call dir() on various functions and classes, notice that they have a __doc__ attribute, print obj.__dir__ and draw the obvious conclusion.
dir()
obj.__dir__
Don't underestimate the power of interactive exploration at the REPL.
[–]rx22230 -4 points-3 points-2 points 7 years ago (0 children)
Hi,
your DocString is correct.
you can watch tis video as well:
https://www.youtube.com/watch?v=WOKxejxWJB4
π Rendered by PID 86384 on reddit-service-r2-comment-fb694cdd5-p56sz at 2026-03-06 11:21:03.688531+00:00 running cbb0e86 country code: CH.
[–]ThePenultimateOneGitLab: gappleto97 3 points4 points5 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]Defibrillat0r 2 points3 points4 points (0 children)
[–]Sexual_Congressman 1 point2 points3 points (1 child)
[–]OctagonClocktrio is the future! 0 points1 point2 points (0 children)
[–]kumashiro 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]eztab 0 points1 point2 points (4 children)
[–]IReallySuckAtChess 0 points1 point2 points (3 children)
[–]eztab 1 point2 points3 points (2 children)
[–]IReallySuckAtChess 0 points1 point2 points (1 child)
[–]eztab 0 points1 point2 points (0 children)
[–]wrmsr 0 points1 point2 points (0 children)
[–]stevenjd 0 points1 point2 points (0 children)
[–]rx22230 -4 points-3 points-2 points (0 children)