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

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

  1. Hello,

    what is the use-case of parameter "start" in string's "endswith"
    method? Consider the following minimal example:

    a = "testing"
    suffix="ing"
    a.endswith(suffix, 2)

    Significance of "end" is obvious. But not so for "start".

    Let's assume the "end" parameter is not used - then the function
    should simple check that the last "len(suffix)" characters of "a" are
    equal to "ing", no matter where we start (the function does not *scan*
    the string from the "start", does it?)
    Only case where it would make difference is if we had start +
    len(suffix) < len(a) (excuse possible "of-by-one" error :)
    Then the function would never return True. But is there a real use
    case when we would test for endswith like this? (knowing that it must
    return false?)

    Thanks for any ideas/experience.
    Boris
     
    =?utf-8?B?Qm9yaXMgRHXFoWVr?=, Apr 19, 2007
    #1
    1. Advertising

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

    Larry Bates Guest

    Boris Dušek wrote:
    > Hello,
    >
    > what is the use-case of parameter "start" in string's "endswith"
    > method? Consider the following minimal example:
    >
    > a = "testing"
    > suffix="ing"
    > a.endswith(suffix, 2)
    >
    > Significance of "end" is obvious. But not so for "start".
    >
    > Let's assume the "end" parameter is not used - then the function
    > should simple check that the last "len(suffix)" characters of "a" are
    > equal to "ing", no matter where we start (the function does not *scan*
    > the string from the "start", does it?)
    > Only case where it would make difference is if we had start +
    > len(suffix) < len(a) (excuse possible "of-by-one" error :)
    > Then the function would never return True. But is there a real use
    > case when we would test for endswith like this? (knowing that it must
    > return false?)
    >
    > Thanks for any ideas/experience.
    > Boris
    >


    Seems like a convenience I've never used.

    >>> a="abcdef"
    >>> a.endswith('cd',2,4)

    True
    >>> a[2:4].endswith('cd')

    True

    -Larry
     
    Larry Bates, Apr 19, 2007
    #2
    1. Advertising

  3. On Apr 19, 3:58 pm, Boris Dušek <> wrote:
    > Hello,
    >
    > what is the use-case of parameter "start" in string's "endswith"
    > method? Consider the following minimal example:
    >
    > a = "testing"
    > suffix="ing"
    > a.endswith(suffix, 2)
    >
    > Significance of "end" is obvious. But not so for "start".
    >
    > Let's assume the "end" parameter is not used - then the function
    > should simple check that the last "len(suffix)" characters of "a" are
    > equal to "ing", no matter where we start (the function does not *scan*
    > the string from the "start", does it?)
    > Only case where it would make difference is if we had start +
    > len(suffix) < len(a) (excuse possible "of-by-one" error :)
    > Then the function would never return True. But is there a real use
    > case when we would test for endswith like this? (knowing that it must
    > return false?)
    >
    > Thanks for any ideas/experience.
    > Boris


    Basically, this must be so in order for this to be Pythonic. This is
    because it is an object oriented language, and functions can be passed
    as arguments. Say, for example, you have the following function:

    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.

    I personally have never used the second or third parameters in this
    function nor in str.startswith.
     
    subscriber123, Apr 19, 2007
    #3
  4. =?utf-8?B?Qm9yaXMgRHXFoWVr?=

    John Machin Guest

    On Apr 20, 6:08 am, Larry Bates <> wrote:
    > Boris Dušek wrote:
    > > Hello,

    >
    > > what is the use-case of parameter "start" in string's "endswith"
    > > method? Consider the following minimal example:

    >
    > > a = "testing"
    > > suffix="ing"
    > > a.endswith(suffix, 2)

    >
    > > Significance of "end" is obvious. But not so for "start".

    >
    > > Let's assume the "end" parameter is not used - then the function
    > > should simple check that the last "len(suffix)" characters of "a" are
    > > equal to "ing", no matter where we start (the function does not *scan*
    > > the string from the "start", does it?)
    > > Only case where it would make difference is if we had start +
    > > len(suffix) < len(a) (excuse possible "of-by-one" error :)
    > > Then the function would never return True. But is there a real use
    > > case when we would test for endswith like this? (knowing that it must
    > > return false?)


    Any or all of a, start and suffix can be variable. I can't see how we
    could know that it must return false (except of course in the trivial
    and useless case that they are all constant). IMHO it's much better in
    general to let a string method do checks for corner cases (very
    efficiently) than to waste brain cells trying to work out how to write
    correct (but relatively very slow) Python code to avoid calling the
    method.

    >
    > Seems like a convenience I've never used.
    >
    > >>> a="abcdef"
    > >>> a.endswith('cd',2,4)

    > True
    > >>> a[2:4].endswith('cd')

    >
    > True
    >


    It's not just a convenience, and not just with endswith. In general:
    astring[start:end].amethod(anarg) manifests the slice as a separate
    temporary object, whereas astring.amethod(anarg, start, stop) does
    not, and should be more efficient when one is looping over a long
    string

    Granted endswith's start arg is not wildly useful, but it's orthogonal
    -- IMHO all string-processing functions should have a pair of start/
    end args as a matter of course, unless neither start nor end is
    useful.

    Cheers,
    John
     
    John Machin, Apr 19, 2007
    #4
  5. =?utf-8?B?Qm9yaXMgRHXFoWVr?=

    John Machin Guest

    On Apr 20, 6:36 am, 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? Consider the following minimal example:

    >
    > > a = "testing"
    > > suffix="ing"
    > > a.endswith(suffix, 2)

    >
    > > Significance of "end" is obvious. But not so for "start".

    >
    > > Let's assume the "end" parameter is not used - then the function
    > > should simple check that the last "len(suffix)" characters of "a" are
    > > equal to "ing", no matter where we start (the function does not *scan*
    > > the string from the "start", does it?)
    > > Only case where it would make difference is if we had start +
    > > len(suffix) < len(a) (excuse possible "of-by-one" error :)
    > > Then the function would never return True. But is there a real use
    > > case when we would test for endswith like this? (knowing that it must
    > > return false?)

    >
    > > Thanks for any ideas/experience.
    > > Boris

    >
    > Basically, this must be so in order for this to be Pythonic. This is
    > because it is an object oriented language, and functions can be passed
    > as arguments. Say, for example, you have the following function:
    >
    > def foo(function,instance,param):
    > if function(instance,param,2,4):
    > return True
    > else: return False


    Perhaps
    return function(instance, param, 2, 4)
    would have a higher pythonicity index :)

    >
    > 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.
    >
    > I personally have never used the second or third parameters in this
    > function nor in str.startswith.
     
    John Machin, Apr 19, 2007
    #5
    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,937
    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:
    809
    Peter Hansen
    Jan 9, 2004
  3. metaperl
    Replies:
    5
    Views:
    319
    Lawrence D'Oliveiro
    Sep 29, 2006
  4. =?utf-8?B?Qm9yaXMgRHXFoWVr?=
    Replies:
    5
    Views:
    349
    Steven D'Aprano
    Apr 21, 2007
  5. Matt Funk

    question about endswith()

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

Share This Page