dml*_*cht 54 python printing pygame
有没有办法阻止函数调用print
?
我正在使用该pygame.joystick
模块进行我正在进行的游戏.
我创建了一个pygame.joystick.Joystick
对象,并在游戏的实际循环中调用其成员函数get_button
来检查用户输入.该功能完成了我需要它做的所有事情,但问题是它也会调用print
,这会大大减慢游戏速度.
我可以阻止这个电话print
吗?
Bri*_*and 64
Python允许您使用任何文件对象覆盖标准输出(stdout).这应该跨平台工作并写入空设备.
import sys, os
# Disable
def blockPrint():
sys.stdout = open(os.devnull, 'w')
# Restore
def enablePrint():
sys.stdout = sys.__stdout__
print 'This will print'
blockPrint()
print "This won't"
enablePrint()
print "This will too"
Run Code Online (Sandbox Code Playgroud)
如果您不希望打印一个函数,请blockPrint()
在它之前调用,以及enablePrint()
何时希望它继续.如果要禁用所有打印,请在文件顶部开始阻止.
Ale*_*hen 55
基于@FakeRainBrigand解决方案,我建议采用更安全的解决方案:
import os, sys
class HiddenPrints:
def __enter__(self):
self._original_stdout = sys.stdout
sys.stdout = open(os.devnull, 'w')
def __exit__(self, exc_type, exc_val, exc_tb):
sys.stdout.close()
sys.stdout = self._original_stdout
Run Code Online (Sandbox Code Playgroud)
然后你可以像这样使用它:
with HiddenPrints():
print("This will not be printed")
print("This will be printed as before")
Run Code Online (Sandbox Code Playgroud)
这样更安全,因为您不能忘记重新启用stdout,这在处理异常时尤其重要.
# This is an example of not-so-good solution
# without 'with' context manager statement.
try:
disable_prints()
something_throwing()
# enable_prints() This wouldn't be enough!
except ValueError:
handle_error()
finally:
enable_prints() # That's where it needs to go.
Run Code Online (Sandbox Code Playgroud)
如果您忘记了该finally
条款,您的所有print
电话都不会打印任何内容.使用该with
声明,这是不可能发生的.
使用它是不安全的sys.stdout = None
,因为有人可以调用sys.stdout.write()之类的方法
Fow*_*ler 10
如果您想阻止特定函数发出的打印调用,则使用装饰器有一个更简洁的解决方案。定义以下装饰器:
# decorater used to block function printing to the console
def blockPrinting(func):
def func_wrapper(*args, **kwargs):
# block all printing to the console
sys.stdout = open(os.devnull, 'w')
# call the method in question
value = func(*args, **kwargs)
# enable all printing to the console
sys.stdout = sys.__stdout__
# pass the return value of the method back
return value
return func_wrapper
Run Code Online (Sandbox Code Playgroud)
然后放在@blockPrinting
任何函数之前。例如:
# This will print
def helloWorld():
print("Hello World!")
helloWorld()
# This will not print
@blockPrinting
def helloWorld2():
print("Hello World!")
helloWorld2()
Run Code Online (Sandbox Code Playgroud)
正如@Alexander Chzhen所建议的那样,使用上下文管理器比调用一对状态更改函数更安全。
但是,您不需要重新实现上下文管理器-它已经在标准库中。您可以重定向stdout
(文件对象print
与使用)contextlib.redirect_stdout
,也stderr
有contextlib.redirect_stderr
。
import os
import contextlib
with open(os.devnull, "w") as f, contextlib.redirect_stdout(f):
print("This won't be printed.")
Run Code Online (Sandbox Code Playgroud)
如果您使用的是 Jupyter Notebook 或 Colab,请使用以下命令:
from IPython.utils import io
with io.capture_output() as captured:
print("I will not be printed.")
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
47958 次 |
最近记录: |