[Python-Dev] Suggested changes to PEP 215

Jason Orendorff jason@jorendorff.com
Mon, 14 Jan 2002 20:25:18 -0600


One of the examples in PEP 215 is a bit wrong, I think.

        >>> print $'\$a'
        5

This should output a backslash before the 5, because the
string '\$a' has a backslash character in it.

Also, for clarity, PEP 215 should explicitly specify
that the substitution only occurs once.  For example:

        # Existing examples
        >>> a, b = 5, 6
        >>> print $'a = $a, b = $b'
        a = 5, b = 6

        [...]
 
        >>> x = "$a"
        >>> print $'x = $x'
        x = $a

Maybe there should also be examples demonstrating that $-strings
adopt the local namespace.

Also, the PEP says:

]       $'a = $a, b = $b'
]
]   could be compiled as though it were the expression
]
]       ('a = ' + str(a) + ', b = ' + str(b))

Consider:

    def f(str):
        # The argument 'str' masks the builtin str() function.
        a, b = find_stuff(str)
        print $'a = $a, b = $b'
        return a, b

It should be specified that $-strings do not use the local
"str" and "unicode" names to find str() and unicode(); nor
do they look in the current __builtins__ or the __builtin__
module.  They should use the actual python C implementations
of str() and unicode().  This can be implemented by putting
a direct reference to str or unicode in the co_consts tuple
of the code object; I don't know how else the author plans
to deal with this.

## Jason Orendorff    http://www.jorendorff.com/