Bad raw string handling, string ends with backslash

Wolfgang wl at flexis.de
Wed May 28 03:06:49 EDT 2003


Bengt Richter wrote:
> On Tue, 27 May 2003 18:04:32 -0400, "Terry Reedy" <tjreedy at udel.edu> wrote:
> 
> 
>>"Wolfgang" <wl at flexis.de> wrote in message
>>>I have to do this:
>>>
>>>xyz = r"T:\bin\bla" + "\\"
>>
>>If this is for accessing directories or files on WindowsXXX,
>>xyz = "T:/bin/bla/"
>>may work (it does for me with Win9X for os.chdir() and file())
>>
> 
> Also note that you don't need the '+', since the tokenizer will join
> adjacent strings separated by whitespace (or no space). And the code
> is a few nanoseconds faster too.
> 
>  >>> import dis
>  >>> dis.dis( lambda: r"T:\bin\bla" + '\\')
>            0 SET_LINENO               1
>            3 LOAD_CONST               1 ('T:\\bin\\bla')
>            6 LOAD_CONST               2 ('\\')
>            9 BINARY_ADD
>           10 RETURN_VALUE
>  >>> dis.dis( lambda: r"T:\bin\bla"   '\\')
>            0 SET_LINENO               1
>            3 LOAD_CONST               1 ('T:\\bin\\bla\\')
>            6 RETURN_VALUE
>  >>> dis.dis( lambda: r"T:\bin\bla""\\")
>            0 SET_LINENO               1
>            3 LOAD_CONST               1 ('T:\\bin\\bla\\')
>            6 RETURN_VALUE
> 
> But what would be wrong with having a version of raw string that
> totally ignored the escape nature of backslashes? So you couldn't
> quote certain combinations of quotes, you could always fall back
> to current status. Say upper case R would work that way, then the OP
> could write
> 
>     R"T:\bin\bla\"
> 
> what was the problem with that again?

Nothing. It's a good idea.

But more special cases in python are not necessary.
For me it's only a minor problem. The first time I discovered
this handling it was a little bit confusing to me.
The solution without a "+" sign is acceptable to me.
(Till not beautiful and "unpythonic")

Now I know how useful a disasembler can be. Thanks.

bye by Wolfgang






More information about the Python-list mailing list