Another surprise from the datetime module

Ben Finney ben+python at benfinney.id.au
Thu Jan 30 19:35:14 EST 2014


Cameron Simpson <cs at zip.com.au> writes:

> Hmm. I do not like the replace() as suggested.
>
> Firstly, replace is a verb, and I would normally read
> td.replace(microseconds=0) as an instruction to modify td in place.
> Traditionally, such methods in python return None.

I agree with this objection. A method that is named “replace”, yet does
not modify the object, is badly named.

However, the existing ‘replace’ methods ‘datetime.date.replace’,
‘datetime.datetime.replace’, ‘datetime.time.replace’ already work this
way: they create a new value and return it, without modifying the
original object.

    <URL:http://docs.python.org/3/library/datetime.html#datetime.date.replace>
    <URL:http://docs.python.org/3/library/datetime.html#datetime.datetime.replace>
    <URL:http://docs.python.org/3/library/datetime.html#datetime.time.replace>

So, if ‘datetime.timedelta.replace’ were to be implemented (I'm not
convinced it is needed), it should have that same behaviour.

-- 
 \      “I tell you the truth: this generation will certainly not pass |
  `\           away until all these things [the end of the world] have |
_o__)   happened.” —Jesus Christ, c. 30 CE, as quoted in Matthew 24:34 |
Ben Finney




More information about the Python-list mailing list