Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > print - bug or feature - concatenated format strings in a printstatement

Reply
Thread Tools

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

 
 
bdb112
Guest
Posts: n/a
 
      03-16-2009
# 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
 
Reply With Quote
 
 
 
 
bdb112
Guest
Posts: n/a
 
      03-16-2009
#whoops, the first output is actually

1, 2, buckle my shoe

# in case it wasn't obvious


On Mar 16, 5:00*pm, bdb112 <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
 
John Machin
Guest
Posts: n/a
 
      03-16-2009
On Mar 16, 7:00*pm, bdb112 <(E-Mail Removed)> 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
 
Reply With Quote
 
R. David Murray
Guest
Posts: n/a
 
      03-16-2009
> On Mar 16, 5:00*pm, bdb112 <(E-Mail Removed)> 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

 
Reply With Quote
 
bdb112
Guest
Posts: n/a
 
      03-18-2009
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 <(E-Mail Removed)> 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


 
Reply With Quote
 
Matt Nordhoff
Guest
Posts: n/a
 
      03-18-2009
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
--
 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      03-18-2009
On Mar 18, 4:19*pm, Matt Nordhoff <(E-Mail Removed)> 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
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      03-18-2009
On Tue, 17 Mar 2009 22:41:26 -0700, John Machin wrote:

> On Mar 18, 4:19*pm, Matt Nordhoff <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bug or feature: double strings as one durumdara Python 20 08-09-2009 11:51 PM
concatenated strings Boris Gambon C Programming 11 06-25-2009 08:30 AM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
When does a bug turn into a design feature? Or, what is a "bug"? robic0 Perl Misc 7 01-24-2006 05:48 AM
Array index bug using concatenated numbers John W. Krahn Perl Misc 5 04-25-2004 11:21 PM



Advertisments