Python! Is! Truly! Amazing!

M

michele.simionato

(Not sure if my other message arrived)

I am guilty of using this idiom, too.

The standard library
http://www.python.org/dev/doc/devel/lib/module-sys.html#l2h-396

says:

"""
__stdin__
__stdout__
__stderr__
These objects contain the original values of stdin, stderr and
stdout at the start of the program. They are used during finalization,
and could be useful to restore the actual files to known working file
objects in case they have been overwritten with a broken object.
"""

Notice the "during finalization" sentence.
Maybe you should change the doc and explain what __stdout__ is intended
for?

Michele Simionato
 
R

Ron Garret

Just said:
Aargh, I can't believe how widespread this idiom is :-(. See my other
reply in this thread: DON'T use sys.__stdout__. Ever.

It's helpful to provide an explanation when saying things like this.

In this case, it's best to save the original value of sys.stdout and
restore that, otherwise nested calls to with_output_to_string can fail,
e.g.

def f():
print 123

def g():
print 456
x = with_output_to_string(f)
print 789

with_outupt_to_string(g) # Will miss the 789

rg
 
R

Ron Garret

jfj said:
I've had it with all those lisp posts lately ;-)

There were functional and non-functional programming languages (the
first being *much* simpler to implement). There is a *reason* people
chose C over lisp. It's not that we were all blind and didn't see the
amazingness of lisp. Procedural languages are simply better, and I'm not
replying to this flamewar.

Then neither am I.

Yes, there's a reason people choose C over Lisp, just as there is a
reason that people choose Windows over OS X, the Ford Taurus over the
Lexus SC400, and Perl over Python. But I'm pretty sure joy isn't that
reason. If joy is part of your quality metric for a programming
language then Lisp is worth a look. (And it costs a lot less than a
Lexus.)

rg
 
J

JanC

Just schreef:
You should always save stdout instead of using __stdout__. It may not be
the same!

You're right, especially when this code would execute in an (at
programming time) unknown context.
 
J

jfj

Ron said:
Just wait until you discover Lisp!

;-)


I've had it with all those lisp posts lately ;-)

There were functional and non-functional programming languages (the
first being *much* simpler to implement). There is a *reason* people
chose C over lisp. It's not that we were all blind and didn't see the
amazingness of lisp. Procedural languages are simply better, and I'm not
replying to this flamewar.

Thank you:)

G.
 
S

Steve Holden

Ron said:
Then neither am I.

Yes, there's a reason people choose C over Lisp, just as there is a
reason that people choose Windows over OS X, the Ford Taurus over the
Lexus SC400, and Perl over Python. But I'm pretty sure joy isn't that
reason. If joy is part of your quality metric for a programming
language then Lisp is worth a look. (And it costs a lot less than a
Lexus.)

Well, in the Computer Programming for Everyone stakes I'm pretty sure
that LISP and its derivatives don't stand a chance, simply because of
the rebarbative syntax one must use to express even relatively simple
algorithms and data structures.

While the language has a lot going for it in the hands of an experienced
and determined LISPer, the average programmer just isn't going to "get it".

regards
Steve
 
S

Simo Melenius

Just said:
Aargh, I can't believe how widespread this idiom is :-(. See my other
reply in this thread: DON'T use sys.__stdout__. Ever.

It probably does the right thing, if someone is copypasting code off
the Usenet and tries it out at the Python prompt (and possibly gets
stuck in getting nothing printed on his tty).

If we're talking about real code it might be advisable to start by
first introducing the import statements omitted from the above
snippet, then worry about someone copypasting code into a serious
program instead of copythinkpasting it. :)

br,
S
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top