Better dev exp
Here we will learn about function annotations, docstrings, and formatting styles.
Function Annotation
- In Python 3 we can specify the type of our arguments and what our function returns.
- They’re optional metadata info.
[!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
andtyping.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
- First line:
- Always short.
- Concise summary of the function’s purpose.
- Begin with a capital letter and end with a period.
- If it is multiline, second line should be blank.
- The 3rd line going forward you can write a description about your function. Things like:
- Calling conventions.
- Return value.
- etc.
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
- Making your code more readable is always a good idea, and adopting a nice coding style can help that tremendously.
- PEP 8 is our style guide as I mentioned it from time to time.
Most Important Points Extracted from PEP 8
- Use docstrings.
- Use 4-space indentation, no tabs.
- Wrap lines so that they do NOT exceed 79 characters.
- Write comments in a separate line whenever possible.
- Use blank lines to separate functions, classes, and larger blocks of code inside functions.
-
Spaces:
- After commas.
- Around operators.
result = max(1, 2) + max(3, 4)
-
Consistent function/class naming:
- Class:
PascalCase
. - Methods/functions:
snake_case
.
We talked about naming conventions here.
- Class:
self
as the name for the first method argument (will discuss it when we learned classes in Python).- Try to use plain English and not fancy characters or some chars that needs a different encoding than utf-8 (we talked about encoding here).
VSCode Extensions
Here we’ll go over some of the most useful extensions in VSCode for coding in Python.