python:将sys.stdout打印更改为自定义打印功能 [英] python: change sys.stdout print to custom print function

查看:271
本文介绍了python:将sys.stdout打印更改为自定义打印功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解如何创建自定义打印功能. (使用python 2.7)

import sys
class CustomPrint():
    def __init__(self):
        self.old_stdout=sys.stdout #save stdout

    def write(self, text):
        sys.stdout = self.old_stdout #restore normal stdout and print
        print 'custom Print--->' + text
        sys.stdout= self # make stdout use CustomPrint on next 'print'
                         # this is the line that trigers the problem
                         # how to avoid this??


myPrint = CustomPrint()
sys.stdout = myPrint
print 'why you make 2 lines??...'

上面的代码将其打印到控制台:

>>> 
custom Print--->why you make 2 lines??...
custom Print--->

>>> 

我只想打印一行:

>>>    
1custom Print--->why you make 2 lines??...
>>>

但是无法弄清楚如何使自定义打印工作,我知道有些递归会触发第二个输出到控制台(我使用self.write,将stdout分配给self.write自己!)

我该如何进行这项工作?还是我的方法完全错误...

解决方案

这不是递归.发生的情况是您的write函数被调用了两次,一次调用了您期望的文本,第二次调用了仅'\n'.试试这个:

import sys
class CustomPrint():
    def __init__(self):
        self.old_stdout=sys.stdout

    def write(self, text):
        text = text.rstrip()
        if len(text) == 0: return
        self.old_stdout.write('custom Print--->' + text + '\n')

我在上面的代码中所做的是我在第一个调用中传递的文本中添加了换行符,并确保由print语句进行的第二个调用(用于打印新行的那个)不会打印任何东西.

现在尝试注释掉前两行,看看会发生什么:

    def write(self, text):
        #text = text.rstrip()
        #if len(text) == 0: return
        self.old_stdout.write('custom Print--->' + text + '\n')

Im trying to understand how to create a custom print function. (using python 2.7)

import sys
class CustomPrint():
    def __init__(self):
        self.old_stdout=sys.stdout #save stdout

    def write(self, text):
        sys.stdout = self.old_stdout #restore normal stdout and print
        print 'custom Print--->' + text
        sys.stdout= self # make stdout use CustomPrint on next 'print'
                         # this is the line that trigers the problem
                         # how to avoid this??


myPrint = CustomPrint()
sys.stdout = myPrint
print 'why you make 2 lines??...'

The code above prints this to console:

>>> 
custom Print--->why you make 2 lines??...
custom Print--->

>>> 

and i want to print only one line:

>>>    
1custom Print--->why you make 2 lines??...
>>>

But cant figure out how to make this custom print work , i understand that there's some kind of recursion that triggers the second output to the console (i use self.write , to assign stdout to self.write himself !)

how can i make this work ? or is my approach just completely wrong...

解决方案

It's not recursion. What happens is your write function is called twice, once with the text you expect, second time with just '\n'. Try this:

import sys
class CustomPrint():
    def __init__(self):
        self.old_stdout=sys.stdout

    def write(self, text):
        text = text.rstrip()
        if len(text) == 0: return
        self.old_stdout.write('custom Print--->' + text + '\n')

What I do in the above code is I add the new line character to the text passed in the first call, and make sure the second call made by the print statement, the one meant to print new line, doesn't print anything.

Now try to comment out the first two lines and see what happens:

    def write(self, text):
        #text = text.rstrip()
        #if len(text) == 0: return
        self.old_stdout.write('custom Print--->' + text + '\n')

这篇关于python:将sys.stdout打印更改为自定义打印功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆