Python

Write an awesome doc for Python. A very nice an practical one extracted from Python official documentation.

View on GitHub

Better dev exp

Here we will learn about function annotations, docstrings, and formatting styles.

Function Annotation

[!NOTE]

Though the main idea was introduced in PEP 3107 and PEP 484 but from version 3.5 onward we have a typing section in the Python’s doc. So be sure to read those PEPs and docs thoroughly since type-safety of your code, and its maintainability depends on it IMO.

def place_order(
        product_id: int,
        quantity: int,
        /,
        discount_code: str=None,
        *,
        shipping_address: str,
        expedited: bool=False) -> None:
    pass

[!TIP]

You’ll learn about modules here, but just for now you just need to know that they are like a toolbox with prebuilt tools ready for you to use them. And in that toolbox we have a tool (module) called typing.

typing.Tuple and typing.List are Generic types; this means you can specify what type their contents must be e.g. a list of integers:

import typing
numbers: typing.List[int] = [1, 2]

But from version 3.5 we do not need to use these generic types exported from typing module. But instead we can use the built-in types, i.e.:

numbers: list[int] = [3, 4]

What is important is to keep in mind that this is the new preferred way of type annotating stuff.

Docstring

def place_order(
        product_id: int,
        quantity: int,
        /,
        discount_code: str=None,
        *,
        shipping_address: str,
        expedited: bool=False) -> None:
    """
    Places an order for a product.

    Arguments:
    - product_id (int): ID of the product (positional-only)
    - quantity (int): Quantity to order (positional-only)
    - discount_code (str): Optional promo code (positional-or-keyword)
    - shipping_address (str): Required keyword-only address
    - expedited (bool): Optional, keyword-only flag for fast shipping
    """
    pass

[!TIP]

You can learn how to write good docstring by going through PEP 0256, PEP 0257 and PEP 0258.

Formatting Styles

Most Important Points Extracted from PEP 8

VSCode Extensions

Here we’ll go over some of the most useful extensions in VSCode for coding in Python.

autopep8 1. Open "Extensions", press `Ctrl+Shift+x` or click on its icon on the left hand side panel: ![Open extensions](/python/02-getting-started/assets/open-extenions.png) 2. Search for "[autopep8](https://marketplace.visualstudio.com/items?itemName=ms-python.autopep8)" and click on install button. ![Search](/python/02-getting-started/assets/search-autopep8.png) 3. Open "Settings" by pressing `Ctrl+comma` or clicking on the gear icon on the bottom left corner of VSCode: ![Open Settings](/python/02-getting-started/assets/open-settings.png) 4. Then search for "Format On Save" and check it: ![Check format on save setting in VSCode](/python/02-getting-started/assets/check-format-on-save.png) 5. Configure autopep8 to be your default code formatter. To do that we need to open command palette by pressing `Ctrl+Shift+p` and type "format document with": ![Configure formatter](/python/02-getting-started/assets/configure-formatter.png) 6. Press enter and autopep8 should be your default formatter. ![Check if autopep8 is your default formatter](/python/02-getting-started/assets/autopep8-should-be-default-formatter.png) If not click on the "Configure Default Formatter": ![Configure autopep8 as your default formatter](/python/02-getting-started/assets/set-autopep8-as-your-default-formatter.png) 7. And we need to configure it to not cross 79 line length define by PEP 8: ![Config autopep8 max-line-length](/python/02-getting-started/assets/config-autopep8-max-line-length.png) [You can learn how to do it step by step here](https://stackoverflow.com/a/74370180/8784518). > [!CAUTION] > > This extension in my experience does not do a very good job. So be sure to read the PEP 8 just in case it was messing with your code.
Pylint Nice suggestions to improve your code quality.
Mypy Type Checker An extension so that when you have a nicely annotated code like this your VSCode shows you a squiggly red line under it, indicating that something is wrong there: ```py var: list[int] = [1, '2', 3] ```
Flake8 An extension for showing errors in your code that are more of a syntactical issue. E.g. here it will draw a squiggly line under `bogus` since it is a valid type. ```py def a(aaaaaaaaaaaaaaa: int, bbbbbbbbbbbbbb: bogus): pass ```
Pylance An extension for showing intellisense. E.g. when you type `"a string"` and press dot it should show a complete list of all methods available in Python.

YouTube/Aparat