RFC PEP candidate: q'<delim>'quoted<delim> ?

Bengt Richter bokr at oz.net
Sun Mar 3 19:28:42 EST 2002


On Sun, 3 Mar 2002 14:56:45 +0300 (MSK), Roman Suzi <rnd at onego.ru> wrote:

>On 3 Mar 2002, Bengt Richter wrote:
>
>>Problem: How to put quotes around an arbitrary program text?
>
>Have it in a separate file.
>
That's fine in a lot of cases, but does require reliable presence
of both files. It's not so convenient if you are trying to write
e.g. a program generator and want to write out snippets containing
mixed triple quoted doc strings and data etc. Or if you are
writing copied example snips as part of dynamic HTML from CGI.

You can say, "Well adopt a consistent methodology, so you don't
try to quote triple quotes with quotes of the same kind."

Ok. But IMO it's easier to think up a safe delimiter for yourself
and use that to quote. Yes, Perlophobes can point to
    $s = <<XXX ;
"""But I'd say convenient precedents were invented for a reason."""
XXX

    s= Q'XXX'
"""This would make it as easy in Python."""
XXX

    s = '''"""This works a lot of the time, so use it when you prefer ;-)"""\n'''
    s = r'''"""Or avoid the \n like this ;-)"""
'''

and, with a simple copy&paste, and I have the above safely in a string:

    snippet = Q'===the stuff I want==='
Ok. But IMO it's easier to think up a safe delimiter for yourself
and use that to quote. Yes, Perlophobes can point to
    $s = <<XXX ;
"""But I'd say convenient precedents were invented for a reason."""
XXX

    s= Q'XXX'
"""This would make it as easy in Python."""
XXX

    s = '''"""This works a lot of the time, so use it when you prefer ;-)"""\n'''
    s = r'''"""Or avoid the \n like this ;-)"""
'''
===the stuff I want===


Sure, I can copy it and make a separate file, and write some code to read the file
into my snippet string, but would you actually prefer to do it that way? Ok, I could
write a class to systematize it. Maybe I will, when I want use file-stored snippets,
but I'd like both options ;-) And I guess I'd like to have Q' as well as q' ;-)

>>Obviously a program may contain quoted material using all the
>>defined string quoting methods (and this new method as well),
>>so the problem is defining delimiters that won't occur in the text.
>>
>>I propose using a variation of the MIME multipart delimiter idea:
>>
>>Note the lack of quotes around the final delimiter string, since it itself is
>>the final delimiter. This can also be used to solve the final unescaped
                                ^^^^
>>backslash problem for quoting windows paths:
>>
>>    q'|'c:\foo\bar\|
>
>Making Python as gibberish as Perl is. And all that only to
>have Windows path be written without double-\
Not 'only'. I said 'also' ;-)  Perhaps my choice of '|' delimiter triggered
your 'gibberish as Perl' detector?

I could have written

    q'###'c:\foo\bar\###
or
    q'[quoting delimiter]'c:\foo\bar\[quoting delimiter] 

just as well for this one.

>AFAIK, latest Windows also use / for delimiting dirs.
>So this is non-problem.
Sure, you have identified a context-dependent non-problem ;-)

Regards,
Bengt Richter




More information about the Python-list mailing list