print의 flush=False 인자

이지훈·2022년 4월 25일
0

파이썬에서 제일 많이 사용하는 함수는 print일 것이다.

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.

print(object(s), separator=separator, end=end, file=file, flush=flush)

pring 함수를 알아보면 다 아는 것이 있다. 근데 flush라는 것이 있는 걸 처음 알았다.

flush는 사용자 입출력(i/O 버퍼)과 관계가 있다.
키보드와 화면 사이에는 '버퍼(buffer)' 개념 또는 장치가 있어서 버퍼에서 내용을 출력 전에 잠시 보관하게 된다.

중요한건 Buffer는 항상 어느정도 차야 출력에 표시가 된다는 것이다. 헌데 Flush=False는 '그것을 무시해버리고 출력해버려라 또는 Buffer을 비워라' 라고 해석할 수 있다.

즉 flush를 써버리면 '위 함수에서 쓰자마자 버려라'라고 해석하면 편하다.


geeksforgeeks 페이지에 나와있는 말이다.

In this program initially, we create gfg.txt file and write Geeks 4 geeks! as content in it and then we close the file. After that we read and display the contents of the file and then the flush() method is called which clears the input buffer of the file so the fileObject reads nothing and fileContent remains an empty variable. Hence nothing is displayed after flush() method.

다시 다시 다시 말하면, 입력버퍼를 지우는 flush 메서드가 호출되서 아무것도 읽지않아서 빈변수로 만들어버린다. 라고 한다.


참고
https://www.geeksforgeeks.org/file-flush-method-in-python/

https://gist.github.com/shoark7/fa0a66bfc37d63890603a276f974f0b6

profile
꾸준하게 🐌

0개의 댓글