FAQ  •  Login

Output Question!

Moderators: UncleTimmy, mjpieters

Enjoying the challenge? Need a hint?
Make a donation and help keep the site running! -thesamet
<<

Memory

Posts: 6

Joined: Sun Nov 13, 2011 12:20 am

Post Sun Nov 13, 2011 12:25 am

Output Question!

How can I print sth without '\n' ?
Now I use print(" ",end="")
but I think it's very inconvenient.
I'm a lazy guy~
Thank you !
<<

blast_hardcheese

Posts: 32

Joined: Wed Aug 17, 2011 12:52 am

Post Sun Nov 13, 2011 1:19 am

Re: Output Question!

Either:

print "blah",

or:

import sys

sys.stdout.write("blah")
sys.stdout.flush()
<<

Memory

Posts: 6

Joined: Sun Nov 13, 2011 12:20 am

Post Sun Nov 13, 2011 3:50 am

Re: Output Question!

I use python 3.x
so it seems that print 'abc', doesn't work out
But the second works! Thanks! :D
And I wonder does the function print use the stdout actually?
<<

blast_hardcheese

Posts: 32

Joined: Wed Aug 17, 2011 12:52 am

Post Mon Nov 14, 2011 12:37 am

Re: Output Question!

Behind the scenes, yes :)

Try:
  Code:
import sys
_stdout = sys.stdout

def write(s):
    _stdout.write(s + "\n")
    _stdout.flush()

class MyClass:
    def __init__(self):
        write("Init!")
    def write(*arg, **kwargs):
        write("Received: " + str(arg) + " and " + str(kwargs))
    def flush(*arg, **kwargs):
        write("Flush!")

sys.stdout = MyClass()

print "Test"
<<

Memory

Posts: 6

Joined: Sun Nov 13, 2011 12:20 am

Post Mon Nov 14, 2011 7:35 am

Re: Output Question!

Wow!That's cool! But it also puzzled me much.
You see,I'm a beginner.So it draws out another problem for me.
Could you explain about the answer a bit?
Here is My output:
  Code:
Init!
Received: (<__main__.MyClass object at 0x01DB44B0>, 'Test') and {}
Received: (<__main__.MyClass object at 0x01DB44B0>, '\n') and {}
Flush!


sys.stdout = MyClass()
what does the constructed function return?
And why and how does the variables *arg, **kwargs will change?
I guess the whole code works like an override or stdout redirection. But I cant figure it out clearly.
<<

blast_hardcheese

Posts: 32

Joined: Wed Aug 17, 2011 12:52 am

Post Mon Nov 14, 2011 1:40 pm

Re: Output Question!

MyClass isn't a function, it's a class

MyClass() returns an instance of that class (First calling __init__, which is why you see "Init!" before anything else)


When you do:
  Code:
print "Test"


what Python is doing is:
  Code:
sys.stdout.write("Test")
sys.stdout.write("\n")
sys.stdout.flush()


The reason args and kwargs change is because you're passing different arguments in, first "Test", then "\n".
<<

Memory

Posts: 6

Joined: Sun Nov 13, 2011 12:20 am

Post Mon Nov 14, 2011 9:34 pm

Re: Output Question!

sys.stdout = MyClass()

Oh,I see.But how can a class do such things?
Why can you assign your class to the stdout?
And doing that makes the original stdout invalid?
<<

blast_hardcheese

Posts: 32

Joined: Wed Aug 17, 2011 12:52 am

Post Mon Nov 21, 2011 12:50 am

Re: Output Question!

Memory wrote:
sys.stdout = MyClass()

Oh,I see.But how can a class do such things?
Why can you assign your class to the stdout?
And doing that makes the original stdout invalid?



I strongly suggest you read about classes, a lot of how Python works will probably seem magical without a solid understanding of them :)

http://docs.python.org/tutorial/classes.html
http://learnpythonthehardway.org/book/ex42.html
http://pytut.infogami.com/node11-baseline.html

Pick one of those and read a bit.

As for your other questions:

Why can you assign your class to the stdout?

Why not?

And doing that makes the original stdout invalid?

I'm not suggesting (at all) that you do this in production code. I'm using this as an example to show you how the print statement works. Fortunately Python as a language allows us this flexibility for experimentation and learning :)
<<

Memory

Posts: 6

Joined: Sun Nov 13, 2011 12:20 am

Post Sun Nov 27, 2011 9:15 am

Re: Output Question!

I'm back. It really help me a lot :) Thank u very much.

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 8 guests

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.