[英]Why does datetime.date not provide strptime?
The python module datetime.datetime
provides strftime
and strptime
. python模块datetime.datetime
提供strftime
和strptime
。
datetime.date
provides only strftime
. datetime.date
仅提供strftime
。
Why is that? 这是为什么?
datetime.strptime
is a class method that generates datetime
objects. datetime.strptime
是一个生成 datetime
对象的类方法 。 Since a datetime
is more general than a date
object and the latter can be retrieved from the former via .date()
and there is no need to reimplement this method (besides having a method called date.strptime
would be quite confusing since it mixes terms "date" and "time" while it's actually just a date). 由于datetime
date
比date
对象更通用,并且可以通过.date()
从date
对象中检索date
对象,因此无需重新实现此方法(除了使用名为date.strptime
的方法之外,因为它混合了术语“日期”和“时间”,而实际上只是一个日期)。
On the other hand datetime.strftime
and date.strftime
are instance methods that transform a given object. 另一方面, datetime.strftime
和date.strftime
是转换给定对象的实例方法 。 In order to provide the same functionality for date
objects this methods needs to reimplemented (also because mapping date -> datetime
is ambiguous). 为了为date
对象提供相同的功能,需要重新实现此方法(同样是因为映射date- date -> datetime
不明确)。
Note that in Python 3.7 there was the date.fromisoformat
classmethod added, as a convenience method for parsing specific date strings (inverse to date.isoformat
). 请注意,在Python 3.7中,添加了date.fromisoformat
类方法,作为解析特定日期字符串(与date.isoformat
)的一种便捷方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.