Re: Mandis Quotes (aka retiring """ and ''')

From: Jeff Epler (jepler_at_unpythonic.net)
Date: 10/04/04


Date: Mon, 4 Oct 2004 10:45:32 -0500
To: Russell Nelson <nelson@crynwr.com>


One problem I see is that certain 'x'-quoted strings are currently legal
Python program fragments. For instance,
    xx = 'x'+'x'
and
    xx = 'x' '' 'x'
(two silly ways to speciy the string 'xx')

A real-life example where "Mandis quotes" would change the meaning of an
existing program:
    def m(rows):
        m = max([len(str(s)) for row in rows for s in row])
        return "\n".join([r(row, "%%%ds" % m) for row in rows])

    def r(row, fmt):
            return '|' + ' '.join([fmt % i for i in row]) + '|'

>>> print m([[1,2,3],[4,5,100]])
| 1 2 3|
| 4 5 100|

I didn't like that r'' strings were added, so I hope that "add one more
kind of string literal" is already dead-in-the-water as a proposal.
Especially since the removal of any kind of string literal is impossible
before Python 3000. The fact that this would change the meaning of
legitimate programs, well, that's even worse.

These quotes are similar to perl's q{} quoting:
       Quote and Quote-like Operators

       While we usually think of quotes as literal values, in Perl they func-
       tion as operators, providing various kinds of interpolating and pattern
       matching capabilities. Perl provides customary quote characters for
       these behaviors, but also provides a way for you to choose your quote
       character for any of them. In the following table, a "{}" represents
       any pair of delimiters you choose.

           Customary Generic Meaning Interpolates
               '' q{} Literal no
               "" qq{} Literal yes
[...]

       Non-bracketing delimiters use the same character fore and aft, but the
       four sorts of brackets (round, angle, square, curly) will all nest,
       which means that

               q{foo{bar}baz}

       is the same as

               'foo{bar}baz'
What advantage do mandis-quotes have over perl's q{}? The bracketing
delimeters rule seems like a handy one when dealing with strings that
might be program source code, mathematical expressions, or even just
paragraphs of text with parentheticals.

Do any major editors (Emacs and Vim to me; others can name their favorite)
treat Mandis-quotes better than they treat triple-quotes? For me,
the poor treatment in text editors of triple-quotes is their biggest
weakness, but parsing mandis-quotes seems no easier, and maybe harder.

Finally, I don't understand why
    ''here's the trick''
wouldn't be a single mandis-quoted string (equal to 'here\'s the trick'
and "here's the trick"), because the delimeter is '', and that
doesn't appear within the string.

Jeff






Relevant Pages

  • A neat trick to serialize arrays and hashes
    ... who thinks that my trick is "obvious to everyone but inexperienced ... hashes -- that is, it can pack and unpack arrays and hashes to and ... I might want to serialize @a into a string for the purpose of storing ... array into a string like so: ...
    (comp.lang.perl.misc)
  • Re: Efficient use of results of [binary scan]
    ... And memory allocation is *definitely* comparatively slow ... The same trick is ... naif solution, for my 100K string. ... DOULOS - Developing Design Know-how ...
    (comp.lang.tcl)
  • Re: Fetch function names & prototypes?
    ... > although in C++ the prototypes are found in the header files included ... > string into the object file symbol table, ... trick would not allow a perfect/accurate type checking. ... arguments have complex data types, ...
    (comp.compilers)
  • Re: Concatenation
    ... You can use a nifty little trick here which utilises the difference between ... "some string" + Null results in Null ...
    (microsoft.public.access.queries)
  • Re: Newbie: Convert String to Symbol?
    ... But I'm missing the conversion from string to symbol! ... (defmacro defop (symbol function) ... this "trick" is very nice when mapping assembler ...
    (comp.lang.lisp)