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

Discussion in 'Python' started by bdb112, Mar 16, 2009.

  1. bdb112

    bdb112 Guest

    # is the difference between
    print(" %d, %d, buckle my shoe" % (1,2))
    # and
    print(" %d, " + " %d, buckle my shoe" % (1,2))
    # a bug or a feature?

    First output
    .... print(" %d " + " %d, buckle my shoe" % (1,2))

    Second output
    TypeError: not all arguments converted during string formatting

    Version Info:
    Python 2.5.1 (r251:54863, Oct 30 2007, 13:54:11)
    [GCC 4.1.2 20070925 (Red Hat 4.1.2-33)] on linux2

    also

    Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit
    (Intel)] on win32
    bdb112, Mar 16, 2009
    #1
    1. Advertising

  2. bdb112

    bdb112 Guest

    #whoops, the first output is actually

    1, 2, buckle my shoe

    # in case it wasn't obvious


    On Mar 16, 5:00 pm, bdb112 <> wrote:
    > #   is the difference between
    > print(" %d,  %d, buckle my shoe" % (1,2))
    > #   and
    > print(" %d, " + " %d, buckle my shoe" % (1,2))
    > # a bug or a feature?
    >
    > First output
    > ... print(" %d " + " %d, buckle my shoe" % (1,2))
    >
    > Second output
    > TypeError: not all arguments converted during string formatting
    >
    > Version Info:
    > Python 2.5.1 (r251:54863, Oct 30 2007, 13:54:11)
    > [GCC 4.1.2 20070925 (Red Hat 4.1.2-33)] on linux2
    >
    > also
    >
    > Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit
    > (Intel)] on win32
    bdb112, Mar 16, 2009
    #2
    1. Advertising

  3. bdb112

    John Machin Guest

    On Mar 16, 7:00 pm, bdb112 <> wrote:
    > #   is the difference between
    > print(" %d,  %d, buckle my shoe" % (1,2))
    > #   and
    > print(" %d, " + " %d, buckle my shoe" % (1,2))
    > # a bug or a feature?


    Here's a question for you:
    Is the difference between
    print(30 % 7)
    and
    print(10 + 20 % 7)
    a bug or a feature?

    Cheers,
    John
    John Machin, Mar 16, 2009
    #3
  4. > On Mar 16, 5:00 pm, bdb112 <> wrote:
    > > #   is the difference between
    > > print(" %d,  %d, buckle my shoe" % (1,2))
    > > #   and
    > > print(" %d, " + " %d, buckle my shoe" % (1,2))
    > > # a bug or a feature?


    It is correct behavior. On the other hand, it is one of the, well,
    bugs, that is avoided by the 'format' method in 3.x.

    --
    R. David Murray http://www.bitdance.com
    R. David Murray, Mar 16, 2009
    #4
  5. bdb112

    bdb112 Guest

    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. (+).

    On Mar 16, 5:00 pm, bdb112 <> wrote:
    > #   is the difference between
    > print(" %d,  %d, buckle my shoe" % (1,2))
    > #   and
    > print(" %d, " + " %d, buckle my shoe" % (1,2))
    > # a bug or a feature?
    >
    > First output
    > ... print(" %d " + " %d, buckle my shoe" % (1,2))
    >
    > Second output
    > TypeError: not all arguments converted during string formatting
    >
    > Version Info:
    > Python 2.5.1 (r251:54863, Oct 30 2007, 13:54:11)
    > [GCC 4.1.2 20070925 (Red Hat 4.1.2-33)] on linux2
    >
    > also
    >
    > Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit
    > (Intel)] on win32
    bdb112, Mar 18, 2009
    #5
  6. 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
    --
    Matt Nordhoff, Mar 18, 2009
    #6
  7. bdb112

    John Machin Guest

    On Mar 18, 4:19 pm, Matt Nordhoff <> 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
    John Machin, Mar 18, 2009
    #7
  8. On Tue, 17 Mar 2009 22:41:26 -0700, John Machin wrote:

    > On Mar 18, 4:19 pm, Matt Nordhoff <> wrote:


    >> 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



    That's the keyhole optimizer in action. It replaces operations on two
    literals at compile-time whenever possible. By memory, that was
    introduced by Python 2.4, and implicit string concatenation was
    introduced way back in the mists of time. In Python 2.1 we have this:


    >>> dis.dis(compile("'ab' 'cd'", '', 'single'))

    0 SET_LINENO 0

    3 SET_LINENO 1
    6 LOAD_CONST 0 ('abcd')
    9 PRINT_EXPR
    10 LOAD_CONST 1 (None)
    13 RETURN_VALUE
    >>>
    >>> dis.dis(compile("1+1", '', 'single'))

    0 SET_LINENO 0

    3 SET_LINENO 1
    6 LOAD_CONST 0 (1)
    9 LOAD_CONST 0 (1)
    12 BINARY_ADD
    13 PRINT_EXPR
    14 LOAD_CONST 1 (None)
    17 RETURN_VALUE



    I suppose, technically, implicit string concatenation could happen inside
    the lexer, or the parser, or some other process, but I think most people
    are happy to simplify all of those as "the compiler". Whenever it
    happens, the important thing is that it is *not* at runtime.



    --
    Steven
    Steven D'Aprano, Mar 18, 2009
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dh
    Replies:
    0
    Views:
    331
  2. Shabam

    Form variable concatenated

    Shabam, Sep 16, 2005, in forum: HTML
    Replies:
    2
    Views:
    407
    Mark Parnell
    Sep 16, 2005
  3. Boris Gambon

    concatenated strings

    Boris Gambon, Jun 23, 2009, in forum: C Programming
    Replies:
    11
    Views:
    524
    Nick Keighley
    Jun 25, 2009
  4. durumdara

    Bug or feature: double strings as one

    durumdara, Aug 7, 2009, in forum: Python
    Replies:
    20
    Views:
    720
  5. John W. Krahn

    Array index bug using concatenated numbers

    John W. Krahn, Apr 25, 2004, in forum: Perl Misc
    Replies:
    5
    Views:
    102
    John W. Krahn
    Apr 26, 2004
Loading...

Share This Page