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

bukzor workitharder at gmail.com
Mon Jul 28 19:30:51 CEST 2008


On Jul 26, 7:08 am, "D'Arcy J.M. Cain" <da... at druid.net> wrote:
> On Sat, 26 Jul 2008 14:07:52 +1000
>
> Ben Finney <bignose+hates-s... 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 <da... at druid.net>         |  Democracy is three wolveshttp://www.druid.net/darcy/               |  and a sheep voting on
> +1 416 425 1212     (DoD#0082)    (eNTP)   |  what's for dinner.

Thanks for backing me up.

Nobody here thinks it's strange that print uses *none* of the
attributes or methods of sys.stdout to do its job? The implementation
seems to bypass the whole python system and use C-level FILE* pointers
directly instead.



More information about the Python-list mailing list