#2
Inspired Python
def current_datetime(dt = datetime.datetime.now()):
    print(dt)

>>> current_datetime()
2021-10-09 16:09:43.009959
>>> current_datetime()
2021-10-09 16:09:43.009959

Python Anti-Patterns: Argument Side Effects


Be extra careful if you assign the result of a function call to a default argument. Python evaluates argument defaults on module load, so if your function is non-deterministic or has side-effects, you could run into trouble. As the example shows, the printed time is frozen; it was set once when the code was first evaluated.

Be Inspired Get Python tips sent to your inbox

We'll tell you about the latest courses and articles.

Absolutely no spam. We promise!