Làm thế nào để xả đầu ra của in python?
Tôi đề nghị năm cách làm điều này:
- Trong Python 3, hãy gọi
3 [đối số tuôn ra không có sẵn trong hàm in của Python 2 và không có tương tự cho câu lệnh in].from __future__ import print_function import sys if sys.version_info[:2] < [3, 3]: old_print = print def print[*args, **kwargs]: flush = kwargs.pop['flush', False] old_print[*args, **kwargs] if flush: file = kwargs.get['file', sys.stdout] # Why might file=None? IDK, but it works for print[i, file=None] file.flush[] if file is not None else sys.stdout.flush[]
- Gọi
4 trên tệp đầu ra [chúng ta có thể kết thúc chức năng in của Python 2 để thực hiện việc này], ví dụ:from __future__ import print_function import sys if sys.version_info[:2] < [3, 3]: old_print = print def print[*args, **kwargs]: flush = kwargs.pop['flush', False] old_print[*args, **kwargs] if flush: file = kwargs.get['file', sys.stdout] # Why might file=None? IDK, but it works for print[i, file=None] file.flush[] if file is not None else sys.stdout.flush[]
5from __future__ import print_function import sys if sys.version_info[:2] < [3, 3]: old_print = print def print[*args, **kwargs]: flush = kwargs.pop['flush', False] old_print[*args, **kwargs] if flush: file = kwargs.get['file', sys.stdout] # Why might file=None? IDK, but it works for print[i, file=None] file.flush[] if file is not None else sys.stdout.flush[]
- Áp dụng điều này cho mọi cuộc gọi chức năng in trong mô -đun với hàm một phần,
6 áp dụng cho mô -đun toàn cầu.from __future__ import print_function import sys if sys.version_info[:2] < [3, 3]: old_print = print def print[*args, **kwargs]: flush = kwargs.pop['flush', False] old_print[*args, **kwargs] if flush: file = kwargs.get['file', sys.stdout] # Why might file=None? IDK, but it works for print[i, file=None] file.flush[] if file is not None else sys.stdout.flush[]
6 applied to the module global.from __future__ import print_function import sys if sys.version_info[:2] < [3, 3]: old_print = print def print[*args, **kwargs]: flush = kwargs.pop['flush', False] old_print[*args, **kwargs] if flush: file = kwargs.get['file', sys.stdout] # Why might file=None? IDK, but it works for print[i, file=None] file.flush[] if file is not None else sys.stdout.flush[]
- Áp dụng điều này cho quy trình với cờ [
7] được chuyển cho lệnh phiên dịchfrom __future__ import print_function import sys if sys.version_info[:2] < [3, 3]: old_print = print def print[*args, **kwargs]: flush = kwargs.pop['flush', False] old_print[*args, **kwargs] if flush: file = kwargs.get['file', sys.stdout] # Why might file=None? IDK, but it works for print[i, file=None] file.flush[] if file is not None else sys.stdout.flush[]
- Áp dụng điều này cho mọi quy trình Python trong môi trường của bạn với
8 [và giải quyết biến để hoàn tác điều này].from __future__ import print_function import sys if sys.version_info[:2] < [3, 3]: old_print = print def print[*args, **kwargs]: flush = kwargs.pop['flush', False] old_print[*args, **kwargs] if flush: file = kwargs.get['file', sys.stdout] # Why might file=None? IDK, but it works for print[i, file=None] file.flush[] if file is not None else sys.stdout.flush[]
Python 3.3+
Sử dụng Python 3.3 trở lên, bạn chỉ có thể cung cấp
from __future__ import print_function
import sys
if sys.version_info[:2] < [3, 3]:
old_print = print
def print[*args, **kwargs]:
flush = kwargs.pop['flush', False]
old_print[*args, **kwargs]
if flush:
file = kwargs.get['file', sys.stdout]
# Why might file=None? IDK, but it works for print[i, file=None]
file.flush[] if file is not None else sys.stdout.flush[]
9 làm đối số từ khóa cho hàm import sys
print 'delayed output'
sys.stdout.flush[]
0:print['foo', flush=True]
Python 2 [hoặc >> print = functools.partial[print, flush=True] >>> print functools.partial[, flush=True]
Chúng ta có thể thấy nó hoạt động giống như bình thường:
>>> print['foo']
foo
Và chúng ta thực sự có thể ghi đè mặc định mới:
>>> print['foo', flush=False]
foo
Lưu ý một lần nữa, điều này chỉ thay đổi phạm vi toàn cầu hiện tại, bởi vì tên in trên phạm vi toàn cầu hiện tại sẽ làm lu mờ hàm
import sys
print 'delayed output'
sys.stdout.flush[]
0 tích hợp [hoặc không liên kết chức năng tương thích, nếu sử dụng một trong Python 2, trong phạm vi toàn cầu hiện tại đó].Nếu bạn muốn thực hiện điều này bên trong một hàm thay vì trên phạm vi toàn cầu của một mô -đun, bạn nên đặt cho nó một tên khác, ví dụ:
def foo[]:
printf = functools.partial[print, flush=True]
printf['print stuff like this']
Nếu bạn tuyên bố nó là toàn cầu trong một hàm, bạn sẽ thay đổi nó trên không gian tên toàn cầu của mô -đun, vì vậy bạn chỉ nên đặt nó vào không gian tên toàn cầu, trừ khi hành vi cụ thể đó chính xác là những gì bạn muốn.
Thay đổi mặc định cho quy trình
Tôi nghĩ rằng tùy chọn tốt nhất ở đây là sử dụng cờ
from __future__ import print_function
import sys
if sys.version_info[:2] < [3, 3]:
old_print = print
def print[*args, **kwargs]:
flush = kwargs.pop['flush', False]
old_print[*args, **kwargs]
if flush:
file = kwargs.get['file', sys.stdout]
# Why might file=None? IDK, but it works for print[i, file=None]
file.flush[] if file is not None else sys.stdout.flush[]
7 để có được đầu ra không bị ảnh hưởng.$ python -u script.py
hoặc
$ python -um package.module
Từ các tài liệu:
Lực lượng Stdin, Stdout và Stderr hoàn toàn không bị cản trở. Trên các hệ thống nơi nó quan trọng, cũng đặt stdin, stdout và stderr ở chế độ nhị phân.
Lưu ý rằng có bộ đệm nội bộ trong File.ReadLines [] và đối tượng tệp [đối với dòng trong sys.stdin] không bị ảnh hưởng bởi tùy chọn này. Để làm việc xung quanh điều này, bạn sẽ muốn sử dụng file.readline [] trong một thời gian 1: vòng lặp.
Thay đổi mặc định cho môi trường hoạt động shell
Bạn có thể có được hành vi này cho tất cả các quy trình Python trong môi trường hoặc môi trường kế thừa từ môi trường nếu bạn đặt biến môi trường thành một chuỗi không trống:
ví dụ: trong Linux hoặc OSX:
from __future__ import print_function
import sys
if sys.version_info[:2] < [3, 3]:
old_print = print
def print[*args, **kwargs]:
flush = kwargs.pop['flush', False]
old_print[*args, **kwargs]
if flush:
file = kwargs.get['file', sys.stdout]
# Why might file=None? IDK, but it works for print[i, file=None]
file.flush[] if file is not None else sys.stdout.flush[]
0hoặc Windows:
from __future__ import print_function
import sys
if sys.version_info[:2] < [3, 3]:
old_print = print
def print[*args, **kwargs]:
flush = kwargs.pop['flush', False]
old_print[*args, **kwargs]
if flush:
file = kwargs.get['file', sys.stdout]
# Why might file=None? IDK, but it works for print[i, file=None]
file.flush[] if file is not None else sys.stdout.flush[]
1Từ các tài liệu:
Pythonunbuffered
Nếu điều này được đặt thành một chuỗi không trống, nó tương đương với việc chỉ định tùy chọn -U.
Phụ lục
Đây là sự trợ giúp về chức năng in từ Python 2.7.12 - Lưu ý rằng không có đối số
import sys
print 'delayed output'
sys.stdout.flush[]
1:from __future__ import print_function
import sys
if sys.version_info[:2] < [3, 3]:
old_print = print
def print[*args, **kwargs]:
flush = kwargs.pop['flush', False]
old_print[*args, **kwargs]
if flush:
file = kwargs.get['file', sys.stdout]
# Why might file=None? IDK, but it works for print[i, file=None]
file.flush[] if file is not None else sys.stdout.flush[]
2