PEP 358 and operations on bytes
Steve Holden
steve at holdenweb.com
Tue Oct 3 22:28:04 EDT 2006
Ben Finney wrote:
> Gabriel G <gabrielg_laburando at yahoo.com.ar> writes:
>
>
>>At Tuesday 3/10/2006 21:52, Ben Finney wrote:
>>
>>
>>>Gerrit Holl <gerrit at nl.linux.org> writes:
>>>
>>>> - str methods endswith, find, partition, replace, split(lines),
>>>> startswith,
>>>> - Regular expressions
>>>
>>>Looking at those, I don't see why they wouldn't be useful for *all*
>>>sequence types. Perhaps there needs to be a 'seq' type containing
>>>those common methods, that is the superclass of 'str', 'bytes',
>>>'list', 'tuple' et cetera.
>>
>>find() could be useful sometimes.
>>But what means partition, replace, split, etc on a generic sequence?
>
>
> >>> "spamandeggs".partition("and")
> ('spam', 'and', 'eggs')
> >>> ["foo", "bar", "spam", "baz", "quux", "wibble"].partition("spam")
> (["foo", "bar"], ["spam"], ["baz", "quux", "wibble"])
>
> >>> "spamandeggs".startswith("spam")
> True
> >>> ["foo", "bar", "spam", "baz", "quux", "wibble"].startswith("foo")
> True
>
> i.e. the 'str' methods can be seen as operating on a sequence of
> characters; their 'str' return values are likewise sequences of
> characters. That can be generalised to a sequence of the same type as
> the instance; so, in the examples above, the 'list' methods operate on
> sub-lists.
>
> Perhaps not all of the mentioned methods make sense for all sequence
> types. But I think it would be useful to have a common set of
> functionality available for all sequence types, in a common ancestor.
>
This would just be bloat without any use cases being demonstrated. What
is your crying need for these methods? The second one, by the way, is
normally spelled:
["foo", "bar", "spam", "baz", "quux", "wibble"][0] == "foo"
Your *real* generalisation of the string method would actually require
you to write
["foo", "bar", "spam", "baz", "quux", "wibble"].startswith(["foo"])
because you also need to be able to write
["foo", "bar", "spam", "baz", "quux", "wibble"].\
startswith(["foo", "bar"])
OK, a startswith() method could return false on a zero-length sequence
to avoid having to program around that corner case, but I question the
need for this. Similarly your first example would need to take a list
argument.
Python didn't get to be the language it is today by adding unnecessary
hypergeneralisations on a whim. Show me how these methods will improve
the daily lives of programmers and I'll champion them to the developers,
but I don't think the world will be beating a path to your door.
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden
More information about the Python-list
mailing list