Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #8427 > unrolled thread
| Started by | pipehappy <pipehappy@gmail.com> |
|---|---|
| First post | 2011-06-24 19:39 -0700 |
| Last post | 2011-06-24 23:54 -0400 |
| Articles | 5 — 5 participants |
Back to article view | Back to comp.lang.python
what's the big deal for print() pipehappy <pipehappy@gmail.com> - 2011-06-24 19:39 -0700
Re: what's the big deal for print() John Gordon <gordon@panix.com> - 2011-06-25 03:23 +0000
Re: what's the big deal for print() steve+comp.lang.python@pearwood.info - 2011-06-25 14:01 +1000
Re: what's the big deal for print() Duncan Booth <duncan.booth@invalid.invalid> - 2011-06-27 08:23 +0000
Re: what's the big deal for print() Terry Reedy <tjreedy@udel.edu> - 2011-06-24 23:54 -0400
| From | pipehappy <pipehappy@gmail.com> |
|---|---|
| Date | 2011-06-24 19:39 -0700 |
| Subject | what's the big deal for print() |
| Message-ID | <d3558c5d-0fe6-4da7-843d-c2f45b2bf869@y13g2000yqy.googlegroups.com> |
Hi, Why people want print() instead of print str? That's not a big deal and the old choice is more natural. Anyone has some clue?
[toc] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2011-06-25 03:23 +0000 |
| Message-ID | <iu3kb0$oh8$1@reader1.panix.com> |
| In reply to | #8427 |
In <d3558c5d-0fe6-4da7-843d-c2f45b2bf869@y13g2000yqy.googlegroups.com> pipehappy <pipehappy@gmail.com> writes:
> Why people want print() instead of print str? That's not a big deal
> and the old choice is more natural. Anyone has some clue?
Because the new Python uses print(). print "str" is the old way.
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | steve+comp.lang.python@pearwood.info |
|---|---|
| Date | 2011-06-25 14:01 +1000 |
| Message-ID | <4e055da9$0$29970$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #8429 |
John Gordon wrote:
> In <d3558c5d-0fe6-4da7-843d-c2f45b2bf869@y13g2000yqy.googlegroups.com>
> pipehappy <pipehappy@gmail.com> writes:
>
>> Why people want print() instead of print str? That's not a big deal
>> and the old choice is more natural. Anyone has some clue?
>
> Because the new Python uses print(). print "str" is the old way.
I think you missed the point of the question, which is, *why* does the new
Python (version 3+) use a function print() instead of a statement?
The problems with print as a statement includes:
It requires special treatment in the compiler, instead of just being an
ordinary function like len(), etc.
It's hard to come up with special syntax to add extra functionality to print
statement. Compare the ugly syntax needed to add support for printing to
writable files in Python 2:
print >>fileobj, arg # what does the mysterious >> syntax mean?
compared to the natural way it works in Python 3:
print(arg, file=fileobj)
Likewise, changing the delimiter between arguments. Python 3 has:
>>> print(1, 2, 3, sep="*")
1*2*3
while Python 2 requires you to generate the string by hand, and then print
it:
>>> print '*'.join('%s' % x for x in (1, 2, 3))
1*2*3
One Frequently Asked Question is "How do I get rid of the newline after
printing?" In Python 2, you leave a comma at the end of the print
statement. What? A comma? How does that make *any* sense at all???
Unfortunately, while that gets rid of the newline, it also leaves spaces
between items:
>>> def example():
... print 1,
... print 2,
... print 3
...
>>> example()
1 2 3
Here's the Python 3 version:
>>> def example():
... print(1, sep='', end='')
... print(2, sep='', end='')
... print(3, sep='')
...
>>> example()
123
To get the same result in Python 2, you have to use sys.stdout.write().
The canonical explanation for why print is now a function is here:
http://www.python.org/dev/peps/pep-3105/
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Duncan Booth <duncan.booth@invalid.invalid> |
|---|---|
| Date | 2011-06-27 08:23 +0000 |
| Message-ID | <Xns9F115F842E5CAduncanbooth@127.0.0.1> |
| In reply to | #8432 |
steve+comp.lang.python@pearwood.info wrote: > Unfortunately, while that gets rid of the newline, it also leaves spaces > between items: > >>>> def example(): > ... print 1, > ... print 2, > ... print 3 > ... >>>> example() > 1 2 3 > > Here's the Python 3 version: > >>>> def example(): > ... print(1, sep='', end='') > ... print(2, sep='', end='') > ... print(3, sep='') > ... >>>> example() > 123 > > > To get the same result in Python 2, you have to use sys.stdout.write (). > That isn't entirely true: you could set the `softspace` attribute on sys.stdout, but that is even messier. >>> def foo(): ... print 1, ... sys.stdout.softspace=0 ... print 2, ... sys.stdout.softspace=0 ... print 3 ... >>> foo() 123 -- Duncan Booth http://kupuguy.blogspot.com
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2011-06-24 23:54 -0400 |
| Message-ID | <mailman.400.1308974092.1164.python-list@python.org> |
| In reply to | #8427 |
On 6/24/2011 10:39 PM, pipehappy wrote: > Hi, > > Why people want print() instead of print str? That's not a big deal > and the old choice is more natural. Anyone has some clue? print as a function instead of a statement is consistent with input as a function, can be overridden with custom versions, can be passed to functions as an argument, and can have options passed as arguments instead of with terrible syntax hacks. -- Terry Jan Reedy
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web