PEP 358 and operations on bytes

Ben Finney bignose+hates-spam at benfinney.id.au
Wed Oct 4 03:41:47 CEST 2006


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.

-- 
 \     "He may look like an idiot and talk like an idiot but don't let |
  `\           that fool you. He really is an idiot."  -- Groucho Marx |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list