semi-concatenated strings

Daniel Fackrell dfackrell at DELETETHIS.linuxmail.org
Fri May 31 15:12:10 EDT 2002


"Grant Griffin" <Grant_member at newsguy.com> wrote in message
news:ad8f7o017mn at drn.newsguy.com...
> In article <mailman.1022863315.9955.python-list at python.org>, Rich says...
> >
> ...
> >Here's why I personally like this:
> >
> >>>>> '%s' 'x' % (5)
> >'5x'
>
> >>>> '%s' + 'x' % (5)
> >Traceback (most recent call last):
> >  File "<stdin>", line 1, in ?
> >TypeError: not all arguments converted
>
> So it looks like the implicit "+" operator has a higher precedence than
the
> explicit "+" operator.  (BTW, who knew that a language that doesn't have
"++"
> would need two string concatenation operators?)

This concatenation has nothing to do with operators.  Operators are acted on
at runtime, this is simply a feature of the parser.

If I knew more about the bytecode that Python generates, I could go in and
show you that:

'a' 'b'

or

'a' # intra-string comment
'b' # post-string comment

produce bytecode for 'ab' (one token), where:

'a' + 'b'

is read in as three distinct tokens representing "'a'", "+", and "'b'".  The
bytecode for this probably looks equivalent to operator.__add__('a', 'b') or
something similar.

I'm probably way off on the particulars, as I've not delved into the parser
at all, but this should give you the idea that this is a compile-time vs.
run-time difference, and has nothing to do with operators or operator
precedence.

Hope this helps.

Daniel Fackrell





More information about the Python-list mailing list