[Python-Dev] Re: Update PEP 292

Tim Peters tim.peters at gmail.com
Fri Aug 20 03:10:56 CEST 2004


[Tim]
>> I do object to this part:
>>
>>      If the $ character appears at the end of the line, or is followed
>>      by any other character than those described above, it is treated
>>      as if it had been escaped, appearing in the resulting string
>>      unchanged.
>>
>> There's already a facility to escape $, and it's dead easy to use.  $
>> isn't a frequently needed character either in most apps.  So escaping
>> $ "by magic" too is, I think, more likely to make typing errors harder
>> to find than to do anyone real good.

[Barry]
> What would you do about $'s at the end of the string?

Celebrate them, I suppose, if they're somehow better than $'s
elsewhere in the string <wink>.

> I think the implementation would be much more complex if you didn't have this
> rule, e.g. you'd have to match \$$ and the $-placeholder regexps would
> basically have to match everything.  Then the .sub call would have to be
> more complex too, because it would have to check for the existence of
> those match groups and then raise an exception.  Or something like that.

Na.  Add a 4th group to the regexp:

   |(\$)

That will reliably match any $, anywhere, that one of the first three
groups doesn't know what to do with.  Then add ", illegal" to the end
of the LHS of the line that unpacks the groups, and then, e.g.,

   if illegal:
       raise ValueError("unsubstitutable $ at index %d" % mo.start(4))

In return for that new line, I'll never have to worry about typing
"${barry)-rulz!" by mistake (depending on the font you're using, you
may or may not have a hard time seeing the typo in that -- and
assuming I didn't intend "${barry)-rulz!" to be the output too, which
it as things are now).


More information about the Python-Dev mailing list