-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
It is strongly recommended that the time module in your datetime library not be named "time", or be merged into the datetime module of datetime #135003
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
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: import datetime as dt
current_time = dt.datetime.now().time()
nine_thirty = dt.time(9, 30)
status = current_time > nine_thirty (typed on phone, so there may be a typo) See https://adamj.eu/tech/2019/09/12/how-i-import-pythons-datetime-module/ |
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 |
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. |
Maybe it is a software design issue. from datetime import time, date # It is ok but it will conflict with the standard library 'import time'
t = time(9, 30)
d = date(2025, 12, 1) or import datetime # ok
t = datetime.time(9, 30)
d = datetime.date(2025, 12, 1) both of the above methods are fine, but the one below is incorrect. from datetime import datetime # error
t = datetime.time(9, 30)
d = datetime.date(2025, 12, 1) A good design can avoid this confusion. Additionally, |
As mentioned, please discuss this at discuss.python.org/c/ideas/6 first. Thanks. |
Uh oh!
There was an error while loading. Please reload this page.
Feature or enhancement
Proposal:
It is strongly recommended that the
time
module in yourdatetime
library not be namedtime
module, or be merged into thedatetime
module ofdatetime
. Because thetime
module ofdatetime
has the same name as thetime
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 thedatetime.time
module various aliases to avoid conflicts.For example, how does python compare whether the current time is past 9:30? The implement is,
The
from datetime import time
will causetime
name conflicts withimport time
which is a lightweight and high-performance standard libraries. But if developers use yourdatetime
module to construct hours, minutes, and seconds with many lines, likenow = datetime.now() hour, minute = now.hour, now.minute
, or string waynine_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 likenine_thirty = time(9, 30)
.Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response
The text was updated successfully, but these errors were encountered: