Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #53347

Re: print function and unwanted trailing space

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <oscar.j.benjamin@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.011
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'interpreter': 0.05; 'output': 0.05; "'python": 0.09; '2to3': 0.09; 'trailing': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'cares': 0.16; 'cc:name:python list': 0.16; 'interpreter,': 0.16; 'invisible': 0.16; 'loop.': 0.16; 'mode,': 0.16; 'statement.': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'true:': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'finished': 0.19; 'aug': 0.22; 'saying': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; "shouldn't": 0.24; 'cc:2**0': 0.24; 'equivalent': 0.26; 'this:': 0.26; 'code:': 0.26; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'function': 0.29; 'feature': 0.29; '(this': 0.29; "doesn't": 0.30; 'message- id:@mail.gmail.com': 0.30; '(which': 0.31; 'gives': 0.31; 'code': 0.31; 'block,': 0.31; 'breaking': 0.31; "d'aprano": 0.31; 'doc': 0.31; 'indentation': 0.31; 'really,': 0.31; 'steven': 0.31; 'there.': 0.32; 'another': 0.32; 'says': 0.33; "i'd": 0.34; 'could': 0.34; 'something': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; '+0200,': 0.36; 'shows': 0.36; 'useful': 0.36; 'error.': 0.37; 'unit': 0.37; 'skip:- 20': 0.37; 'two': 0.37; 'list,': 0.38; 'space': 0.40; 'even': 0.60; 'august': 0.61; 'strictly': 0.61; 'course': 0.61; 'first': 0.61; 'real': 0.63; 'skip:n 10': 0.64; 'became': 0.64; 'here': 0.66; 'between': 0.67; 'statement,': 0.68; 'difference.': 0.84; 'oscar': 0.84; 'subject:space': 0.84; 'try,': 0.84; 'careful': 0.91; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=Zk9J2eNiWAeLzRG/1aryBrURc5W+312LepWTwFzBwP8=; b=uMZuGh+YxaUehxKeuyOMLk5guM7isZxr5gAYVd7qtbCNseNwXCqRAcQjHF/ps7wxjV YeupaCROalEr4+/taVjzUzhBkt7PWwM1i6IIYIsqFCaZ21PxrZJoc8euPvPktlzl7Tim o2fPSeg6PdUCnVYs2m4dBfGhslkg7sClXJFF/Hr91vo/PPVdAxKlJ2pPe/bDyH5neINw r+zKjNkalzBjFYK97NnwfwvEOGIpuVGfk9X7db/VxzSLvjwGkwwTMSumqpMp8iZRY0Wb utMlac64VV09LvbdEB8Bz9S9UI/ISnUMQR7rnQtXSfmM1ejhCso4plsfo9itSU0PIYxR uYIw==
X-Received by 10.52.244.116 with SMTP id xf20mr10431080vdc.16.1377952698430; Sat, 31 Aug 2013 05:38:18 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <5221d090$0$6599$c3e8da3$5496439d@news.astraweb.com>
References <5221a693$0$2059$426a74cc@news.free.fr> <5221d090$0$6599$c3e8da3$5496439d@news.astraweb.com>
From Oscar Benjamin <oscar.j.benjamin@gmail.com>
Date Sat, 31 Aug 2013 13:37:58 +0100
Subject Re: print function and unwanted trailing space
To "Steven D'Aprano" <steve+comp.lang.python@pearwood.info>
Content-Type text/plain; charset=ISO-8859-1
Cc Python List <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.410.1377952706.19984.python-list@python.org> (permalink)
Lines 75
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1377952706 news.xs4all.nl 15878 [2001:888:2000:d::a6]:52275
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:53347

Show key headers only | View raw


On 31 August 2013 12:16, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> On Sat, 31 Aug 2013 10:17:23 +0200, candide wrote:
>
>> What is the equivalent in Python 3 to the following Python 2 code:
>>
>> # -----------------------------
>> for i in range(5):
>>      print i,
>> # -----------------------------
>>
>> ?
>>
>> Be careful that the above code doesn't add a trailing space after the
>> last number in the list,
>
> Of course it does. Have you actually tried it? The interactive
> interpreter is tricky, because you cannot directly follow a for-loop with
> another statement. If you try, the interactive interpreter gives you an
> indentation error. But we can work around it by sticking everything
> inside an if block, like so:
>
> py> if True:
> ...     for i in range(5):
> ...             print i,
> ...     # could be pages of code here
> ...     print "FINISHED"
> ...
> 0 1 2 3 4 FINISHED

The space is added by the final print statement, not the last one in
the loop. Here's a demo that shows this:

$ cat print.py
for i in range(5):
    print i,
print
$ cat print3.py
for i in range(5):
    print(i, end=' ')
print()
$ py -2.7 print.py | cat -A
0 1 2 3 4^M$
$ py -3.3 print3.py | cat -A
0 1 2 3 4 ^M$

(Notice the space between the 4 and ^M (which is cat's way of saying '\r').

[snip]
>
>> hence the following Python 3 code isn't strictly equivalent:
>>
>>
>> # -----------------------------
>> for i in range(5):
>>      print(i, end=' ')   # <- The last ' ' is unwanted
>> print()
>
> The last space is exactly the same as you get in Python 2. But really,
> who cares about an extra invisible space? In non-interactive mode, the
> two are exactly the same (ignoring the extra print() call outside the
> loop), but even at the interactive interpreter, I'd like to see the code
> where an extra space makes a real difference.

I seem to remember it breaking some unit test or doc test something
when I first tried to port something using 2to3 (this is the
replacement that 2to3 uses for print with a trailing comma). It's not
so important for interactive terminal output but when you do 'python
script.py > output.dat' the unwanted space shouldn't be there. The
soft-space feature is useful but stateful as Peter says and defies the
normal concept of what happens when calling a function so it was
removed when print became a function.


Oscar

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

print function and unwanted trailing space candide <candide@free.invalid> - 2013-08-31 10:17 +0200
  Re: print function and unwanted trailing space Andreas Perstinger <andipersti@gmail.com> - 2013-08-31 10:43 +0200
    Re: print function and unwanted trailing space candide <candide@free.invalid> - 2013-08-31 11:25 +0200
      Re: print function and unwanted trailing space Peter Otten <__peter__@web.de> - 2013-08-31 12:31 +0200
        Re: print function and unwanted trailing space candide <candide@free.invalid> - 2013-08-31 15:33 +0200
          Re: print function and unwanted trailing space Peter Otten <__peter__@web.de> - 2013-08-31 15:59 +0200
            Re: print function and unwanted trailing space candide <candide@free.invalid> - 2013-08-31 17:58 +0200
          Re: print function and unwanted trailing space Chris Angelico <rosuav@gmail.com> - 2013-09-01 01:30 +1000
          Re: print function and unwanted trailing space Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-08-31 16:43 +0100
          Re: print function and unwanted trailing space Chris Angelico <rosuav@gmail.com> - 2013-09-01 08:08 +1000
          Re: print function and unwanted trailing space Joshua Landau <joshua@landau.ws> - 2013-09-01 00:15 +0100
          Re: print function and unwanted trailing space Terry Reedy <tjreedy@udel.edu> - 2013-08-31 19:57 -0400
        Re: print function and unwanted trailing space candide <candide@free.invalid> - 2013-08-31 17:51 +0200
  Re: print function and unwanted trailing space Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-31 11:16 +0000
    Re: print function and unwanted trailing space Peter Otten <__peter__@web.de> - 2013-08-31 14:27 +0200
    Re: print function and unwanted trailing space Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-08-31 13:37 +0100
    Re: print function and unwanted trailing space candide <candide@free.invalid> - 2013-08-31 14:59 +0200
      Re: print function and unwanted trailing space Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-31 15:18 +0000
  Re: print function and unwanted trailing space Ned Batchelder <ned@nedbatchelder.com> - 2013-08-31 07:24 -0400
    Re: print function and unwanted trailing space candide <candide@free.invalid> - 2013-08-31 15:51 +0200
  Re: print function and unwanted trailing space Wayne Werner <wayne@waynewerner.com> - 2013-09-11 06:36 -0500
  Re: print function and unwanted trailing space Albert Hopkins <marduk@letterboxes.org> - 2013-09-12 15:25 -0400

csiph-web