From c9a1168c7034a08801396eaa6396d7af8ae8fdf6 Mon Sep 17 00:00:00 2001 From: Roshan Sharma Date: Mon, 16 Sep 2019 19:27:02 +0530 Subject: [PATCH] README: Add new example Add example - Python breaks order because of unflushed output buffer. Fixes #140 --- README.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/README.md b/README.md index d66d632..4c346d6 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ So, here we go... - [▶ Subclass relationships *](#-subclass-relationships-) - [▶ The mysterious key type conversion *](#-the-mysterious-key-type-conversion-) - [▶ Let's see if you can guess this?](#-lets-see-if-you-can-guess-this) + - [▶ You need to follow order, Python!](#-you-need-to-follow-order-python-) - [Section: Appearances are deceptive!](#section-appearances-are-deceptive) - [▶ Skipping lines?](#-skipping-lines) - [▶ Teleportation *](#-teleportation-) @@ -1221,6 +1222,42 @@ a, b = a[b] = {}, 5 --- +### ▶ You need to follow order, Python! * + +```py +# wtf.py + +import time + +print('hello', end='') + +time.sleep(5) +``` + +**Output:** +```sh +$ python3 wtf.py +``` + +```py +# 5-second delay +hello +``` + +**Unexpected output** +Ideally, `sleep` delay should start after the `print`. Here, just the opposite happens. + +#### 💡 Explanation: + +* The `end` argument in `print` accounts for all this difference. +* When `end` is set to something other than the default `\n`, +the print buffer does not flush, and nothing prints. +* Until the buffer encounters a `\n`, all output is stored in the buffer. +* When the delay stops, and the program comes to an end - the output is flushed, +causing `hello` to print up on the screen. + +--- + --- ## Section: Appearances are deceptive!