Description
Feature or enhancement
Proposal:
It is strongly recommended that the time
module in your datetime
library not be named time
module, or be merged into the datetime
module of datetime
. Because the time
module of datetime
has the same name as the time
module of the standard library and has low performance, the latter is more commonly used. If the former is used, organizations generally do not want to give the datetime.time
module various aliases to avoid conflicts.
For example, how does python compare whether the current time is past 9:30? The implement is,
from datetime import datetime, time
current_time = datetime.now().time()
nine_thirty = time(9, 30)
status = current_time > nine_thirty
The from datetime import time
will cause time
name conflicts with import time
which is a lightweight and high-performance standard libraries. But if developers use your datetime
module to construct hours, minutes, and seconds with many lines, like now = datetime.now() hour, minute = now.hour, now.minute
, or string way nine_thirty = datetime.strptime("09:30:00", "%H:%M:%S").time()
, it will be very troublesome and far less elegant than a single line of code like nine_thirty = time(9, 30)
.
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response
Activity
hugovk commentedon Jun 1, 2025
This needs discussing at https://discuss.python.org/c/ideas/6 first, but it is unlikely to be changed, to maintain backwards compatibility.
I recommend using this pattern to avoid ambiguity:
(typed on phone, so there may be a typo)
See https://adamj.eu/tech/2019/09/12/how-i-import-pythons-datetime-module/
skirpichev commentedon Jun 1, 2025
datetime.time is not a module - it's a class:
>>> import datetime >>> datetime.time <class 'datetime.time'>
Yes, this name clashes with the time module. It happens in other places of the stdlib as well. E.g. math.log and cmath.log.
Renaming (how?) will be a backward compatibility break. Why do you think it worth? To avoid conflict you might create alias like
from datetime import time as badtime
.zyy37 commentedon Jun 1, 2025
You can close it, but you will still encounter many people like me who provide feedback on this issue in the future, just like the one in your link. This shows that the problem is not root cause, it is just a compromise.
zyy37 commentedon Jun 1, 2025
Maybe it is a software design issue.
For example,
or
both of the above methods are fine, but the one below is incorrect.
A good design can avoid this confusion. Additionally,
time()
is sometimes a get property and sometimes a constructor. Going back to the beginning, what I meant was that constructingdate
ortime
objects was originally a very simple scenario, but it caused so much trouble...... Thanks.hugovk commentedon Jun 1, 2025
As mentioned, please discuss this at discuss.python.org/c/ideas/6 first. Thanks.