print - bug or feature - concatenated format strings in a print statement

John Machin sjmachin at lexicon.net
Wed Mar 18 01:41:26 EDT 2009


On Mar 18, 4:19 pm, Matt Nordhoff <mnordh... at mattnordhoff.com> wrote:
> bdb112 wrote:
> > Thanks for all the replies:
> > I think I see now - % is a binary operator whose precedence rules are
> > shared with the modulo operator regardless of the nature of its
> > arguments, for language consistency.
> > I understand the arguments behind the format method, but hope that the
> > slightly idiosyncratic print( ..% ..) remains, as the vaguely
> > pictorial "printf" format string is clearer for a long line with
> > several arguments.
> > I will use the "implicit string concatenation" to solve my problem but
> > it is a little odd that an "invisible" operator is stronger than a
> > visible one. (+).
>
> The implicit string concatenation is actually done by the compiler; it
> isn't an operator at all. Look:
>
> >>> import dis
> >>> def f():
>
> ...     return "foo" "bar"
> ...>>> dis.dis(f)
>
>   2           0 LOAD_CONST               1 ('foobar')
>               3 RETURN_VALUE
> --

I think you need better evidence than that obtained by proctologising
about with dis.dis:

Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit
(Intel)] onwin32
Type "help", "copyright", "credits" or "license" for more information.
>>> import dis
>>> def f():
...    return ('foo') + ('bar')
...
>>> dis.dis(f)
  2           0 LOAD_CONST               3 ('foobar')
              3 RETURN_VALUE
>>>

Cheers,
John



More information about the Python-list mailing list