Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Python have both a module and a class called datetime? [closed]

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 use dt.datetime, etc... because I want to keep the original class names while minimizing the confusion with the module name.

like image 918
tadasajon Avatar asked Oct 09 '14 21:10

tadasajon


People also ask

Is datetime a module in Python?

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.

What is the difference between datetime and time module in Python?

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.

What is the use of date/time module in Python?

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.

What is datetime datetime now () in Python?

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.


1 Answers

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.

like image 163
Jonathan Eunice Avatar answered Oct 12 '22 19:10

Jonathan Eunice