Why does Python have both a module and a class named datetime?
I am constantly confused about whether I imported datetime
from datetime
, or just imported datetime
. If I see in some code what is in use datetime
, I cannot quickly tell if it is a datetime module or a datetime class. So annoying. What can I do?
Additionally, PEP 8 clearly states : "Class names should generally use the CapWords convention."
It would help if the classes in the module datetime
were called datetime
, Date
and Time
.
Since this question was suspended as "not clear what you are asking about" I decided to edit and write more. To be honest, I wasn't sure what I was asking - I just found that I stumbled across this issue repeatedly and wanted feedback. I think I got some very helpful advice though, especially from tdelaney:
Personally, I do
import datetime as dt
usedt.datetime
etc ... because I want to keep the original class names, minimizing the module name confusion.
source to share
It might be nice to have a more consistent naming convention. But the Python standard library and what is classically thought of as "built-in" Python types are neither PEP-8 nor a strict consistency model. All standard types (e.g., int
, float
, str
, list
and dict
) are in lower case. Some extensions dict
are CapWorded, for example OrderedDict
. But his companion is defaultdict
not. There are several types of data that are named the same as their modules, such as array.array
and datetime.datetime
. The main Python 3 census moved some modules around, but didn't homogenize or reorganize the class names to some degree.
So ... just learn to live with him. This is a historical fact and is unlikely to change soon.
source to share