defaultdict – Python dict with default values for absent keys.

Python comprehensions breakdown

Using Python decorators to process and authorize requests

Proper way to get UTC datetime is (docs ), not datetime.utcnow().

Python gotchas:

  • By default datetime is naive, i.e. without a timezone. Naive datetime is treated as local time. This is the source of many, many pains if not treated properly.

  • Even datetime.utcnow() is naive, surprisingly. Meaning it will be treated as local time by default.

  • Python evaluates default function arguments on their definition and not on each function call. Thus, do not use mutable values for default arguments def fn(a=[]), unless you really need a shot in the foot a singleton. Use the following workaround:

    def fn(a=None):
        if a is None:
            a = []

    For dataclasses in can be done with a default_factory in field:

    from dataclasses import dataclass, field
    class Demo:
        a = field(default_factory=list)