Re: Significance of "start" parameter to string method "endswith"

Discussion in 'Python' started by =?utf-8?B?Qm9yaXMgRHXFoWVr?=, Apr 19, 2007.

  1. On Apr 19, 10:36 pm, subscriber123 <> wrote:
    > On Apr 19, 3:58 pm, Boris Dušek <> wrote:
    >
    >
    >
    > > Hello,

    >
    > > what is the use-case of parameter "start" in string's "endswith"
    > > method?


    > def foo(function,instance,param):
    > if function(instance,param,2,4):
    > return True
    > else: return False
    >
    > The function must work whether you pass it
    > foo(str.endswith,"blaahh","ahh"), or
    > foo(str.startswith,"blaahh","aah"). This is a really bad example, but
    > it gets the point across that similar functions must have similar
    > parameters in order to be Pythonic.


    Thanks for explanation, this point makes sense. And I agree that I can
    hardly imagine any use of both parameters :).
     
    =?utf-8?B?Qm9yaXMgRHXFoWVr?=, Apr 19, 2007
    #1
    1. Advertising

  2. On Thu, 19 Apr 2007 13:57:16 -0700, Boris Dusek wrote:

    >> > what is the use-case of parameter "start" in string's "endswith"
    >> > method?

    >
    >> def foo(function,instance,param):
    >> if function(instance,param,2,4):
    >> return True
    >> else: return False
    >>
    >> The function must work whether you pass it
    >> foo(str.endswith,"blaahh","ahh"), or
    >> foo(str.startswith,"blaahh","aah"). This is a really bad example, but
    >> it gets the point across that similar functions must have similar
    >> parameters in order to be Pythonic.

    >
    > Thanks for explanation, this point makes sense. And I agree that I can
    > hardly imagine any use of both parameters :).


    No, sorry, it doesn't make sense because not all string methods take the
    same arguments! See, for instance, ''.translate() and ''.lower().

    The best reason for giving string methods and functions start and end
    parameters is to avoid copying potentially large lumps of text. Here's a
    silly example. Instead of doing this:

    while text:
    p = text.find('parrot')
    buffer = text[:p]
    text = text[p:]
    do_something_with(buffer)

    You can do this:

    p = 0
    while text:
    p = text.find('parrot', p)
    do_something_with(buffer, p)

    which avoids copying text unnecessarily.

    Now, obviously there are better ways to re-write that code (e.g. by
    splitting the text into chunks with split) but it should illustrate the
    concept.


    --
    Steven.
     
    Steven D'Aprano, Apr 20, 2007
    #2
    1. Advertising

  3. =?utf-8?B?Qm9yaXMgRHXFoWVr?=

    John Machin Guest

    On Apr 20, 9:12 am, Steven D'Aprano
    <> wrote:
    > On Thu, 19 Apr 2007 13:57:16 -0700, Boris Dusek wrote:
    > >> > what is the use-case of parameter "start" in string's "endswith"
    > >> > method?

    >
    > >> def foo(function,instance,param):
    > >> if function(instance,param,2,4):
    > >> return True
    > >> else: return False

    >
    > >> The function must work whether you pass it
    > >> foo(str.endswith,"blaahh","ahh"), or
    > >> foo(str.startswith,"blaahh","aah"). This is a really bad example, but
    > >> it gets the point across that similar functions must have similar
    > >> parameters in order to be Pythonic.

    >
    > > Thanks for explanation, this point makes sense. And I agree that I can
    > > hardly imagine any use of both parameters :).

    >
    > No, sorry, it doesn't make sense because not all string methods take the
    > same arguments! See, for instance, ''.translate() and ''.lower().
    >
    > The best reason for giving string methods and functions start and end
    > parameters is to avoid copying potentially large lumps of text. Here's a
    > silly example. Instead of doing this:
    >
    > while text:
    > p = text.find('parrot')
    > buffer = text[:p]
    > text = text[p:]
    > do_something_with(buffer)
    >
    > You can do this:
    >
    > p = 0
    > while text:
    > p = text.find('parrot', p)
    > do_something_with(buffer, p)
    >
    > which avoids copying text unnecessarily.


    .... but increases the care and attention required when coding:

    (1) "while text"? "while 1" is a more obvious way of stress-testing
    your CPU fan :) but perhaps you meant "while p >= 0".

    (2) 4s/buffer/text/

    Cheers,
    John
     
    John Machin, Apr 20, 2007
    #3
  4. On Fri, 20 Apr 2007 15:21:56 -0700, John Machin wrote:

    > On Apr 20, 9:12 am, Steven D'Aprano
    > <> wrote:
    >> On Thu, 19 Apr 2007 13:57:16 -0700, Boris Dusek wrote:
    >> >> > what is the use-case of parameter "start" in string's "endswith"
    >> >> > method?

    >>
    >> >> def foo(function,instance,param):
    >> >> if function(instance,param,2,4):
    >> >> return True
    >> >> else: return False

    >>
    >> >> The function must work whether you pass it
    >> >> foo(str.endswith,"blaahh","ahh"), or
    >> >> foo(str.startswith,"blaahh","aah"). This is a really bad example, but
    >> >> it gets the point across that similar functions must have similar
    >> >> parameters in order to be Pythonic.

    >>
    >> > Thanks for explanation, this point makes sense. And I agree that I can
    >> > hardly imagine any use of both parameters :).

    >>
    >> No, sorry, it doesn't make sense because not all string methods take the
    >> same arguments! See, for instance, ''.translate() and ''.lower().
    >>
    >> The best reason for giving string methods and functions start and end
    >> parameters is to avoid copying potentially large lumps of text. Here's a
    >> silly example. Instead of doing this:
    >>
    >> while text:
    >> p = text.find('parrot')
    >> buffer = text[:p]
    >> text = text[p:]
    >> do_something_with(buffer)
    >>
    >> You can do this:
    >>
    >> p = 0
    >> while text:
    >> p = text.find('parrot', p)
    >> do_something_with(buffer, p)
    >>
    >> which avoids copying text unnecessarily.

    >
    > ... but increases the care and attention required when coding:


    There are always trade-offs.


    >
    > (1) "while text"? "while 1" is a more obvious way of stress-testing
    > your CPU fan :) but perhaps you meant "while p >= 0".


    No, I meant exactly what I said. You might notice that text becomes
    smaller after each iteration:

    text = text[p:]

    Eventually text becomes the empty string and the while loop is exited
    cleanly.

    "while p >= 0" would be an alternative, but you still need to copy chars
    into a buffer, and you still need to shrink the text or else you'll just
    keep getting the same p over and over again.


    > (2) 4s/buffer/text/



    Yes, a silly typo caused by copy-and-paste-without-proof-reading.



    --
    Steven.
     
    Steven D'Aprano, Apr 21, 2007
    #4
  5. En Fri, 20 Apr 2007 23:13:23 -0300, Steven D'Aprano
    <> escribió:

    > On Fri, 20 Apr 2007 15:21:56 -0700, John Machin wrote:
    >
    >> On Apr 20, 9:12 am, Steven D'Aprano
    >> <> wrote:
    >>>
    >>> You can do this:
    >>>
    >>> p = 0
    >>> while text:
    >>> p = text.find('parrot', p)
    >>> do_something_with(buffer, p)
    >>>
    >>> which avoids copying text unnecessarily.

    >>
    >> ... but increases the care and attention required when coding:

    >
    > There are always trade-offs.


    .... and you still need more attention when replying - read again your own
    reply :)

    --
    Gabriel Genellina
     
    Gabriel Genellina, Apr 21, 2007
    #5
  6. On Fri, 20 Apr 2007 23:20:39 -0300, Gabriel Genellina wrote:

    >>> ... but increases the care and attention required when coding:

    >>
    >> There are always trade-offs.

    >
    > ... and you still need more attention when replying - read again your own
    > reply :)



    D'oh!



    --
    Steven.
     
    Steven D'Aprano, Apr 21, 2007
    #6
    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. Replies:
    17
    Views:
    1,874
    Chris Uppal
    Nov 16, 2005
  2. Michele Simionato

    feature request: a better str.endswith

    Michele Simionato, Jul 18, 2003, in forum: Python
    Replies:
    24
    Views:
    779
    Peter Hansen
    Jan 9, 2004
  3. metaperl
    Replies:
    5
    Views:
    303
    Lawrence D'Oliveiro
    Sep 29, 2006
  4. =?utf-8?B?Qm9yaXMgRHXFoWVr?=

    Significance of "start" parameter to string method "endswith"

    =?utf-8?B?Qm9yaXMgRHXFoWVr?=, Apr 19, 2007, in forum: Python
    Replies:
    4
    Views:
    437
    John Machin
    Apr 19, 2007
  5. Matt Funk

    question about endswith()

    Matt Funk, Mar 3, 2011, in forum: Python
    Replies:
    5
    Views:
    266
    HMX962b
    Mar 4, 2011
Loading...

Share This Page