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 datetime.now(timezone.utc)
(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 needa shot in the foota singleton. Use the following workaround:def fn(a=None): if a is None: a = []
For
dataclasses
in can be done with adefault_factory
infield
:from dataclasses import dataclass, field @dataclass class Demo: a = field(default_factory=list)