473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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

Apr 19 '07 #1
4 3264
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
Apr 19 '07 #2
On Apr 19, 3:58 pm, Boris Dušek <boris.du...@gmail.comwrote:
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.

Apr 19 '07 #3
On Apr 20, 6:08 am, Larry Bates <larry.ba...@websafe.comwrote:
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

Apr 19 '07 #4
On Apr 20, 6:36 am, subscriber123 <collinsto...@gmail.comwrote:
On Apr 19, 3:58 pm, Boris Dušek <boris.du...@gmail.comwrote:


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.

Apr 19 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tian | last post by:
I am writing a python program which needs to support some plug-ins. I have an XML file storing some dynamic structures. XML file records some class names whose instance needs to be created in the...
3
by: jty202 | last post by:
I encounter a problem. I have three files: index.aspx index.aspx.vb HTMLContentParser.vb (doesn't have the class WebForm1 I put all three file in the same directory. when I ran...
7
by: Dave Benjamin | last post by:
There's been a lot of discussion lately regarding Ruby and the notion of a "humane" interface to objects like arrays and maps, as opposed to "minimalist" ones. I believe the article that started...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.