Skip to content

Commit 449d045

Browse files
[3.12] gh-115684: Clarify datetime replace documentation (GH-116519)
* Clarify datetime `replace` documentation In GH-115684, HopedForLuck noted that `datetime.date.replace()` documentation was confusing because it looked like it would be changing immutable objects. This documentation change specifies that the `replace()` methods in `datetime` return new objects. This uses similar wording to the documentation for `datetime.combine()`, which specifies that a new datetime is returned. This is also similar to wording for `string.replace()`, except `string.replace()` emphasizes that a "copy" is returned. Resolves GH-115684. * Include reviewer comments Thanks Privat33r-dev for the comments! --------- (cherry picked from commit d2d8862) Co-authored-by: David Lowry-Duda <[email protected]> Co-authored-by: Paul Ganssle <[email protected]>
1 parent 8159805 commit 449d045

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

Doc/library/datetime.rst

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -650,8 +650,8 @@ Instance methods:
650650

651651
.. method:: date.replace(year=self.year, month=self.month, day=self.day)
652652

653-
Return a date with the same value, except for those parameters given new
654-
values by whichever keyword arguments are specified.
653+
Return a new :class:`date` object with the same values, but with specified
654+
parameters updated.
655655

656656
Example::
657657

@@ -660,6 +660,9 @@ Instance methods:
660660
>>> d.replace(day=26)
661661
datetime.date(2002, 12, 26)
662662

663+
The generic function :func:`copy.replace` also supports :class:`date`
664+
objects.
665+
663666

664667
.. method:: date.timetuple()
665668

@@ -1274,10 +1277,10 @@ Instance methods:
12741277
hour=self.hour, minute=self.minute, second=self.second, microsecond=self.microsecond, \
12751278
tzinfo=self.tzinfo, *, fold=0)
12761279
1277-
Return a datetime with the same attributes, except for those attributes given
1278-
new values by whichever keyword arguments are specified. Note that
1279-
``tzinfo=None`` can be specified to create a naive datetime from an aware
1280-
datetime with no conversion of date and time data.
1280+
Return a new :class:`datetime` object with the same attributes, but with
1281+
specified parameters updated. Note that ``tzinfo=None`` can be specified to
1282+
create a naive datetime from an aware datetime with no conversion of date
1283+
and time data.
12811284

12821285
.. versionchanged:: 3.6
12831286
Added the *fold* parameter.
@@ -1849,10 +1852,10 @@ Instance methods:
18491852
.. method:: time.replace(hour=self.hour, minute=self.minute, second=self.second, \
18501853
microsecond=self.microsecond, tzinfo=self.tzinfo, *, fold=0)
18511854
1852-
Return a :class:`.time` with the same value, except for those attributes given
1853-
new values by whichever keyword arguments are specified. Note that
1854-
``tzinfo=None`` can be specified to create a naive :class:`.time` from an
1855-
aware :class:`.time`, without conversion of the time data.
1855+
Return a new :class:`.time` with the same values, but with specified
1856+
parameters updated. Note that ``tzinfo=None`` can be specified to create a
1857+
naive :class:`.time` from an aware :class:`.time`, without conversion of the
1858+
time data.
18561859

18571860
.. versionchanged:: 3.6
18581861
Added the *fold* parameter.

0 commit comments

Comments
 (0)