[Python-Dev] PEP 460: allowing %d and %f and mojibake

Guido van Rossum guido at python.org
Tue Jan 14 16:59:44 CET 2014

On Tue, Jan 14, 2014 at 12:20 AM, Greg Ewing
<greg.ewing at canterbury.ac.nz> wrote:
> Guido van Rossum wrote:
>> I've now looked at asciistr. (Thanks Glenn and Ethan for the link.)
>> Now that I (hopefully) understand it, I'm worried that a text
>> processing algorithm that uses asciistr might under hard-to-predict
>> circumstances (such as when the arguments contain nothing of interest
>> to the algorithm) might return an asciistr instance instead of a str
>> or bytes instance,
> It seems to me that any algorithm with that property
> has a genuine ambiguity as to what it should return
> in that case. Arguably, returning an asciistr would
> be the *right* thing to do, because that would allow
> it to be used as a component of a larger algorithm
> that was polymorphic with respect to text/bytes.

Here's an example of what I mean:

def spam(a):
    r = asciistr('(')
    if a: r += a.strip()
    r += asciistr(')')
    return r

The argument must be a string.

If I call spam(''), a's type is never concatenated with r, so the
return value is an asciistr. To fix this particular case, we could
drop the "if a:" part. But it could be more significant, e.g. it could
be something like "if a contains any digits". The general fix would be
to add

    else: r += a[:0]

but that's still an example of the awkwardness that asciistr() is
trying to avoid.

--Guido van Rossum (python.org/~guido)

More information about the Python-Dev mailing list