Skip to content

Commit

Permalink
README: Add new example
Browse files Browse the repository at this point in the history
Add example - Python breaks order because of unflushed output buffer.

Fixes satwikkansal#140
  • Loading branch information
roshnet committed Sep 16, 2019
1 parent 9f5dbbe commit c9a1168
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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-)
Expand Down Expand Up @@ -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!
Expand Down

0 comments on commit c9a1168

Please sign in to comment.