Re: Another surprise from the datetime module

Discussion in 'Python' started by Steven D'Aprano, Jan 31, 2014.

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

    > Cameron Simpson <> 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
     
    Steven D'Aprano, Jan 31, 2014
    #1
    1. Advertising

  2. Steven D'Aprano

    Roy Smith Guest

    In article <52eb1e37$0$29972$c3e8da3$>,
    Steven D'Aprano <> 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.
     
    Roy Smith, Jan 31, 2014
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christos TZOTZIOY Georgiou
    Replies:
    3
    Views:
    710
    Christos TZOTZIOY Georgiou
    Sep 13, 2003
  2. Tim Peters
    Replies:
    0
    Views:
    536
    Tim Peters
    Sep 9, 2003
  3. Roy Smith
    Replies:
    2
    Views:
    76
    Neil Cerutti
    Jan 30, 2014
  4. Cameron Simpson

    Re: Another surprise from the datetime module

    Cameron Simpson, Jan 31, 2014, in forum: Python
    Replies:
    0
    Views:
    58
    Cameron Simpson
    Jan 31, 2014
  5. Cameron Simpson

    Re: Another surprise from the datetime module

    Cameron Simpson, Jan 31, 2014, in forum: Python
    Replies:
    0
    Views:
    69
    Cameron Simpson
    Jan 31, 2014
Loading...

Share This Page