Yes, because it shows up in editors when hovering. We're not forced, but as a lead, I encourage it.
Docstrings are important and their role is different than comments. They give an overview how the function works, explain parameters and sometimes give examples how to use them. You can generate documentation using sphinx from Docstrings, and as others said IDEs use docstrings to show you information about function you're using.
Comments are just text that gets ignored by interpreter. You can add explanation to code if it's not self explanatory, but they're not useful outside of the immediate area being commented on.
Other languages also have documentation options that is separate from comments, e.g. JavaDoc. The syntax is usually similar to coment, but slightly different to differentiate it from comments (extra * in multiline comment).
Yeah, they're definitely still the standard. They're not really replaced by comments, comments are more for explaining why bits of code are the way they are. Docstrings are kind of like comments for functions/classes/etc that Python knows how to handle specially. The interpreter will parse the docstrings and make help text out of them available to the help
builtin function
At my work we use linters that refuse code with missing or bad docstrings.
Can you share what linters you use?
Most projects are migrating towards Ruff. All in one and speedy.
Yeah, I just moved our linting/format checking pipeline to ruff and it's been painless and fast.
Yes, use them. One big advantage is if you hover something in an IDE it will show you the docstring.
If you're writing Python you should be using Pylint (or Ruff) and it has a lint to ensure you write them.
The exception I usually make is for class member variables because it's super weird that the docstring comes after the variable. I think that's very confusing for people reading the code so I normally just use comments instead.
Yup, use docstrings and be descriptive. Learn by reading docstrings in other codebases. (Django's codebase is always an excellent example of how to do Python, including how to do docstrings.) a) Anyone who sees your code will thank you and b) if you can't explain it in human language, you probably don't understand it well enough to implement it well.
Sounds like they're still very relevant and very important. Python isn't a language I've used a lot but I'm still surprised I've never heard about docstrings till this tutorial. Thanks for the info.
Yes they are sometimes used, as is doctest, but Python's built in help system isn't as good as it could be, so the docstrings aren't that useful. Most annoyingly, for built in library classes, help(whatever) spews machine generated prototypes at you before you get any actual documentation.
I generally like to write some kind of explanatory text along with any nontrivial function that I write, but I'm not very consistent about doing this as a doctring vs as a code comment. I do use type annotations heavily nowadays.
yes
Python
Welcome to the Python community on the programming.dev Lemmy instance!
📅 Events
Past
November 2023
- PyCon Ireland 2023, 11-12th
- PyData Tel Aviv 2023 14th
October 2023
- PyConES Canarias 2023, 6-8th
- DjangoCon US 2023, 16-20th (!django 💬)
July 2023
- PyDelhi Meetup, 2nd
- PyCon Israel, 4-5th
- DFW Pythoneers, 6th
- Django Girls Abraka, 6-7th
- SciPy 2023 10-16th, Austin
- IndyPy, 11th
- Leipzig Python User Group, 11th
- Austin Python, 12th
- EuroPython 2023, 17-23rd
- Austin Python: Evening of Coding, 18th
- PyHEP.dev 2023 - "Python in HEP" Developer's Workshop, 25th
August 2023
- PyLadies Dublin, 15th
- EuroSciPy 2023, 14-18th
September 2023
- PyData Amsterdam, 14-16th
- PyCon UK, 22nd - 25th
🐍 Python project:
- Python
- Documentation
- News & Blog
- Python Planet blog aggregator
💓 Python Community:
- #python IRC for general questions
- #python-dev IRC for CPython developers
- PySlackers Slack channel
- Python Discord server
- Python Weekly newsletters
- Mailing lists
- Forum
✨ Python Ecosystem:
🌌 Fediverse
Communities
- #python on Mastodon
- c/django on programming.dev
- c/pythorhead on lemmy.dbzer0.com
Projects
- Pythörhead: a Python library for interacting with Lemmy
- Plemmy: a Python package for accessing the Lemmy API
- pylemmy pylemmy enables simple access to Lemmy's API with Python
- mastodon.py, a Python wrapper for the Mastodon API