Hướng dẫn how do you flush a python terminal? - làm thế nào để bạn tuôn ra một thiết bị đầu cuối python?

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
    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()
    
    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).
  • Gọi
    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()
    
    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()
    
    5
  • Á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,
    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 á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.
  • Áp dụng điều này cho quy trình với 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 chuyển cho lệnh phiên dịch
  • Á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
    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()
    
    8 (và giải quyết biến để hoàn tác điều này).

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

Họ đã không gửi lại đối số

import sys
print 'delayed output'
sys.stdout.flush()
1 cho Python 2.7, vì vậy nếu bạn đang sử dụng Python 2 (hoặc dưới 3.3) và muốn mã tương thích với cả 2 và 3, tôi có thể đề xuất mã tương thích sau. (Lưu ý nhập
import sys
print 'delayed output'
sys.stdout.flush()
2 phải ở/rất "gần đầu mô -đun của bạn"):

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()

Mã tương thích ở trên sẽ bao gồm hầu hết các mục đích sử dụng, nhưng để xử lý kỹ lưỡng hơn nhiều, hãy xem mô -đun

import sys
print 'delayed output'
sys.stdout.flush()
3.

Ngoài ra, bạn chỉ có thể gọi

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()
4 sau khi in, ví dụ, với câu lệnh in trong Python 2:

import sys
print 'delayed output'
sys.stdout.flush()

Thay đổi mặc định trong một mô -đun thành 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

Bạn có thể thay đổi mặc định cho chức năng in bằng cách sử dụng functools.partial trên phạm vi toàn cầu của mô -đun:

import functools
print = functools.partial(print, flush=True)

Nếu bạn nhìn vào chức năng một phần mới của chúng tôi, ít nhất là trong Python 3:

>>> 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()
0

hoặ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()
1

Từ 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

Nó có nghĩa là gì khi xả Python đầu ra?

Phương pháp in Pythons như một thuộc tính độc quyền là Flush cho phép người dùng quyết định xem anh ta có muốn đầu ra của mình được đệm hay không.Giá trị mặc định của điều này là sai nghĩa là đầu ra sẽ được đệm.Nếu bạn thay đổi nó thành True, đầu ra sẽ được viết dưới dạng một chuỗi các ký tự từng nhân vật.allows the user to decide if he wants his output to be buffered or not. The default value of this is False meaning the output will be buffered. If you change it to true, the output will be written as a sequence of characters one after the other.

Chức năng của Flush () là gì?

Hàm Flush () yêu cầu máy chủ gửi đầu ra được đệm hiện tại đến trình duyệt.requests the server to send its currently buffered output to the browser.

Đầu ra Flush có nghĩa là gì?

Đầu ra xả trên luồng được đệm có nghĩa là truyền tất cả các ký tự tích lũy vào tệp.Có nhiều trường hợp khi đầu ra được đệm trên luồng được tự động xả: khi bạn cố gắng thực hiện đầu ra và bộ đệm đầu ra đã đầy.transmitting all accumulated characters to the file. There are many circumstances when buffered output on a stream is flushed automatically: When you try to do output and the output buffer is full.

SEP = 'có nghĩa là gì trong Python?

sep = '' trong bối cảnh của một cuộc gọi hàm đặt đối số được đặt tên thành một chuỗi trống.Xem hàm in ();SEP là bộ phân cách được sử dụng giữa nhiều giá trị khi in.sets the named argument sep to an empty string. See the print() function; sep is the separator used between multiple values when printing.