[Python-Dev] Proof of the pudding: str.partition()

Fredrik Lundh fredrik at pythonware.com
Wed Aug 31 12:16:51 CEST 2005

Ron Adam wrote:

> I'm not familiar with piece, but it occurred to me it might be useful to
> get attributes groups in some way.  My first (passing) thought was to do...
>      host, port = host.partition(':').(head, sep)
> Where that would be short calling a method to return them:
>      host, port = host.partition(':').getattribs('head','sep')

note, however, that your first syntax doesn't work in today's python
(bare names are always evaluated in the current scope, before any calls
are made)

given that you want both the pieces *and* a way to see if a split was
made, the only half-reasonable alternatives to "I can always ignore the
values I don't need" that I can think of are

    flag, part1, part2, ... = somestring.partition(sep, count=2)


    flag, part1, part2, ... = somestring.piec^H^H^Hartition(sep, group, group, ...)

where flag is true if the separator was found, and the number of parts
returned corresponds to either count or the number of group indices
(the latter is of course the external influence that cannot be named,
but with an API modelled after RE's group method).


More information about the Python-Dev mailing list