adamj.eu

Old Father Datetime

Python’s datetime module risks a whole bunch of name confusion:

  • It contains a class also called datetime. When you read the word datetime, you can’t be sure it’s the module or the class.
  • It contains a time class. This can be confused with the time module, or the time module’s time() function.
  • It contains a class called timezone. In a Django project this can be confused with the django.utils.timezone module.

For these reasons, I use this import idiom and recommend you do too:

import datetime as dt

Rather than any of:

import datetime
from datetime import datetime  # or time, timezone

Then in your code, dt.datetime, dt.time, and dt.timezone will be unambiguous.

Fin

Hope this helps you save time understanding code,

—Adam


Check out my new book Boost Your GitHub DX.


One summary email a week, no spam, I pinky promise.

Related posts:

Tags: django, python

Read the original on adamj.eu ↗