Harper
Harper

Reputation: 1223

Why does datetime.date not provide strptime?

The python module datetime.datetime provides strftime and strptime.

datetime.date provides only strftime.

Why is that?

Upvotes: 5

Views: 651

Answers (1)

a_guest
a_guest

Reputation: 36239

datetime.strptime is a class method that generates datetime objects. 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).

On the other hand datetime.strftime and date.strftime are instance methods that transform a given object. In order to provide the same functionality for date objects this methods needs to reimplemented (also because mapping date -> datetime is ambiguous).

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).

Upvotes: 5

Related Questions