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,
Are your Django project's tests slow? Read Speed Up Your Django Tests now!
One summary email a week, no spam, I pinky promise.
- Tuples versus Lists in Python
- Solving Algorithmic Problems in Python with Pytest
- Limit Your Try Clauses in Python
© 2020 All rights reserved.