binding names doesn't affect the bound objects (was: print doesn't respect file inheritance?)

D'Arcy J.M. Cain darcy at druid.net
Sat Jul 26 10:08:09 EDT 2008


On Sat, 26 Jul 2008 14:07:52 +1000
Ben Finney <bignose+hates-spam at benfinney.id.au> wrote:
> >     sys.stdout = n
> 
> Re-binds the name 'sys.stdout' to the object already referenced by the
> name 'n'. No objects are changed by this; only bindings of names to
> objects.

I do agree that the object formerly known as sys.stdout hasn't changed.

> >     print "Testing: 1, 2, 3..."
> 
> Doesn't rely at all on the name 'sys.stdout', so isn't affected by all
> the binding of names above.

Hmm.  Are you saying that the following doesn't work?

$ python
>>> f = open("test", "w")
>>> import sys
>>> sys.stdout = f
>>> print "test message"
>>> sys.exit(0)
$ cat test
test message

> In other words, you can't change the object used by the 'print'
> statement only by re-binding names (which is *all* that is done by the
> '=' operator).

Apparently I can.

> You can, however, specify which file 'print' should use
> <URL:http://www.python.org/doc/ref/print.html>.

Which contains this statement.

"Standard output is defined as the file object named stdout in the
built-in module sys."

I suppose that there might be some ambiguity there but the proof, as
they say, is in the pudding.

-- 
D'Arcy J.M. Cain <darcy at druid.net>         |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 425 1212     (DoD#0082)    (eNTP)   |  what's for dinner.



More information about the Python-list mailing list