How I Import Python's datetime Module2019-09-12
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
timeclass. This can be confused with the
timemodule, or the
- It contains a class called
timezone. In a Django project this can be confused with the
For these reasons, I use this import idiom and recommend you do too:
Rather than any of:
Then in your code,
dt.timezone will be unambiguous.
Hope this helps you save
time understanding code,
Or perhaps you'd like to read a related post:
- Tuples versus Lists in Python
- Solving Algorithmic Problems in Python with Pytest
- Getting a Django Application to 100% Coverage
© 2019 All rights reserved.