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

subscriber123 collinstocks at gmail.com
Thu Apr 19 16:36:11 EDT 2007


On Apr 19, 3:58 pm, Boris Dušek <boris.du... at gmail.com> 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.




More information about the Python-list mailing list