[Python-ideas] startsin ?

Steven D'Aprano steve at pearwood.info
Sat Oct 1 04:46:13 CEST 2011


Matt Chaput wrote:
> On 30/09/2011 11:30 AM, Tarek Ziadé wrote:
>> not sure how people do this, or if I missed something obvious in the
>> stdlib, but I often have this pattern:
> 
> str's interface is a bit cluttered with some questionable methods 
> ("captialize"? "center"? "swapcase"? "title"?) that probably should have 
> been functions in a text module instead of methods.

The str methods started off as functions in the string module before 
becoming methods in Python 2.0.

[steve at sylar python]$ python1.5 -c "'a'.upper()"
Traceback (innermost last):
   File "<string>", line 1, in ?
AttributeError: 'string' object has no attribute 'upper'

What you describe as "questionable methods" go back to the string 
module, and were made methods deliberately. And so they should be.



> One thing is that the equivalent of .startsin() for .endswith() would be 
> .endsin(). In English, "ends in" is a variation of "ends with", e.g. 
> "What words end in 'a'?"

[pedant]
That may or may not be common in some dialects (although not any I'm 
familiar with, which isn't very many), but it isn't semantically 
correct. The Melbourne to Sydney marathon ends *in* Sydney because the 
place where it ends is *inside* Sydney; a pencil ends *with* a point 
because the end of the pencil *is* a point, it is NOT inside the point. 
Similarly, the word "race" ends *with* an 'e'.



-- 
Steven



More information about the Python-ideas mailing list