[Python-ideas] Make non-meaningful backslashes illegal in string literals
M.-A. Lemburg
mal at egenix.com
Fri Aug 7 11:55:39 CEST 2015
On 07.08.2015 09:15, Chris Angelico wrote:
> On Fri, Aug 7, 2015 at 3:12 PM, Steven D'Aprano <steve at pearwood.info> wrote:
>> On Thu, Aug 06, 2015 at 12:26:14PM -0400, random832 at fastmail.us wrote:
>>> On Wed, Aug 5, 2015, at 14:56, Eric V. Smith wrote:
>>>> Because strings containing \{ are currently valid
>>>
>>> Which raises the question of why.
>>
>> Because \C is currently valid, for all values of C. The idea is that if
>> you typo an escape, say \d for \f, you get an obvious backslash in your
>> string which is easy to spot.
>>
>> Personally, I think that's a mistake. It leads to errors like this:
>>
>> filename = 'C:\some\path\something.txt'
>>
>> silently doing the wrong thing. If we're going to change the way escapes
>> work, it's time to deprecate the misfeature that \C is a literal
>> backslash followed by C. Outside of raw strings, a backslash should
>> *only* be allowed in an escape sequence.
>
> I agree; plus, it means there's yet another thing for people to
> complain about when they switch to Unicode strings:
>
> path = "c:\users", "C:\Users" # OK on Py2
> path = u"c:\users", u"C:\Users" # Fails
Um, Windows path names should always use the raw format:
path = r"c:\users"
Doesn't work with Unicode in Py2, though:
path = ur"c:\users"
on the plus side, you get a SyntaxError right away.
> Or equivalently, moving to Py3 and having those strings quietly become
> Unicode strings, and now having meaning on the \U and \u escapes.
Same as above... use raw format in Py3:
path = r"c:\users"
(only now you get a raw Unicode string; this was changed in Py3
compared to Py2)
--
Marc-Andre Lemburg
eGenix.com
Professional Python Services directly from the Source (#1, Aug 07 2015)
>>> Python Projects, Coaching and Consulting ... http://www.egenix.com/
>>> mxODBC Plone/Zope Database Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
________________________________________________________________________
::::: Try our mxODBC.Connect Python Database Interface for free ! ::::::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
http://www.egenix.com/company/contact/
More information about the Python-ideas
mailing list