I'm constantly getting confused about whether I have imported datetime
from datetime
, or whether I've just imported datetime
. If I see in some code that datetime
is being used, I can't quickly tell whether it is the module datetime or the class datetime. So annoying. What can I do?
Also, PEP 8 clearly states: "Class names should normally use the CapWords convention."
It would help if the classes in the datetime
module were called DateTime
, Date
, and Time
.
Since this question was put on hold as "unclear what you're asking", I've decided to edit and write more. To be honest, I wasn't sure what I was asking either -- I just found that I was repeatedly being tripped up by this issue and wanted to get feedback. I think I did get some very helpful advice, though, especially from tdelaney:
Personally I
import datetime as dt
and usedt.datetime
, etc... because I want to keep the original class names while minimizing the confusion with the module name.
In the Python programming language, datetime is a single module. This means that it is not two separate data types. You can import this datetime module in such a way that they work with dates and times. datetime is a built-in Python module.
time – An idealized time, independent of any particular day, assuming that every day has exactly 24*60*60 seconds. Its attributes are hour, minute, second, microsecond, and tzinfo. datetime – Its a combination of date and time along with the attributes year, month, day, hour, minute, second, microsecond, and tzinfo.
The datetime module supplies classes for manipulating dates and times. While date and time arithmetic is supported, the focus of the implementation is on efficient attribute extraction for output formatting and manipulation. General calendar related functions.
Here, we have used datetime.now() to get the current date and time. Then, we used strftime() to create a string representing date and time in another format.
It might be nice to have more consistent naming. But the standard Python library and what are classically thought of as Python's "built in" types abide neither PEP-8 or any strict model of consistency. All of the standard types (e.g. int
, float
, str
, list
, and dict
) are lower-case. Some extensions of dict
are CapWorded, like OrderedDict
. But its companion defaultdict
is not. There are a number of data types named the same as their modules, such as array.array
and datetime.datetime
. The major rewrite of Python 3 moved some modules around, but it didn't homogenize or reorganize the class names to any great degree.
So...just learn to live with it. It's a historical fact, and not likely to change soon.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With