Another surprise from the datetime module

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Jan 30 22:53:27 EST 2014


On Fri, 31 Jan 2014 11:35:14 +1100, Ben Finney wrote:

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


py> 'badly named'.replace('badly', 'well')
'well named'


"replace" is a perfectly reasonable name for a method which performs a 
replacement, whether it replaces in place (for mutable objects) or makes 
a copy with replacement (for immutable objects). What else would you call 
it?

py> ('well named'.
...  make_a_copy_while_simultaneously_performing_a_replacement_on_the_copy
...  ('well', 'excessively long')
...  )
'excessively long named'

While explicit is better than implicit, sometimes you can be *too* 
explicit.


If timedelta objects were mutable, then I would expect that you would 
just write the fields directly:

td.microseconds = 0

rather than mess about with a replace method.



-- 
Steven



More information about the Python-list mailing list