Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #65075 > unrolled thread

Re: Another surprise from the datetime module

Started byBen Finney <ben+python@benfinney.id.au>
First post2014-01-31 11:35 +1100
Last post2014-01-31 04:04 +0000
Articles 5 — 4 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Another surprise from the datetime module Ben Finney <ben+python@benfinney.id.au> - 2014-01-31 11:35 +1100
    Re: Another surprise from the datetime module Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-01-31 03:53 +0000
      Re: Another surprise from the datetime module Roy Smith <roy@panix.com> - 2014-01-30 22:58 -0500
      Re: Another surprise from the datetime module Ben Finney <ben+python@benfinney.id.au> - 2014-01-31 15:40 +1100
    Re: Another surprise from the datetime module Dan Sommers <dan@tombstonezero.net> - 2014-01-31 04:04 +0000

#65075 — Re: Another surprise from the datetime module

FromBen Finney <ben+python@benfinney.id.au>
Date2014-01-31 11:35 +1100
SubjectRe: Another surprise from the datetime module
Message-ID<mailman.6184.1391128528.18130.python-list@python.org>
Cameron Simpson <cs@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

[toc] | [next] | [standalone]


#65081

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-01-31 03:53 +0000
Message-ID<52eb1e37$0$29972$c3e8da3$5496439d@news.astraweb.com>
In reply to#65075
On Fri, 31 Jan 2014 11:35:14 +1100, Ben Finney wrote:

> Cameron Simpson <cs@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

[toc] | [prev] | [next] | [standalone]


#65082

FromRoy Smith <roy@panix.com>
Date2014-01-30 22:58 -0500
Message-ID<roy-85E1FB.22581030012014@news.panix.com>
In reply to#65081
In article <52eb1e37$0$29972$c3e8da3$5496439d@news.astraweb.com>,
 Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote:

> "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?

I suppose that by (imperfect) analogy to list.sort() and sorted(list), 
it might make more sense to call it replaced().  But, it's too late for 
any of that now.  The other three classes in the module have replace(), 
so that's the obvious name to use for the fourth class.

[toc] | [prev] | [next] | [standalone]


#65087

FromBen Finney <ben+python@benfinney.id.au>
Date2014-01-31 15:40 +1100
Message-ID<mailman.6189.1391143222.18130.python-list@python.org>
In reply to#65081
Steven D'Aprano <steve+comp.lang.python@pearwood.info> writes:

> On Fri, 31 Jan 2014 11:35:14 +1100, Ben Finney wrote:
>
> > Cameron Simpson <cs@zip.com.au> writes:
> >> 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.
>
> […] What else would you call it?

I'd call it “substitute”, and keep thinking until I came up with
something better.

I wouldn't think very hard, though, because the existing usage of
‘foo.replace’ for a create-new-object-with-different-values method is
fairly well established in the Python built-in types and standard
library. Local expectations do, past some threshold, override general
expectations for the meaning of a term.

-- 
 \       “Everyone is entitled to their own opinions, but they are not |
  `\            entitled to their own facts.” —US Senator Pat Moynihan |
_o__)                                                                  |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#65083

FromDan Sommers <dan@tombstonezero.net>
Date2014-01-31 04:04 +0000
Message-ID<lcf7bt$gbt$1@dont-email.me>
In reply to#65075
On Fri, 31 Jan 2014 11:35:14 +1100, Ben Finney wrote:

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

That's how str.replace works, too.

*sigh*

Dan

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web