Skip to content

Commit c84c827

Browse files
committed
Add new example: Teleportation
1 parent 118f51d commit c84c827

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,6 +1530,36 @@ tuple()
15301530
15311531
---
15321532
1533+
### Teleportation
1534+
1535+
Suggested in [this](https://www.reddit.com/r/Python/comments/6x6upn/wtfpython_a_collection_of_interesting_and_tricky/dme96dq/) reddit thread.
1536+
1537+
```py
1538+
import numpy as np
1539+
1540+
def energy_send(x):
1541+
np.array([float(x)])
1542+
1543+
def energy_receive():
1544+
return np.empty((), dtype=np.float).tolist()
1545+
```
1546+
1547+
**Output:**
1548+
```py
1549+
>>> energy_send(123.456)
1550+
>>> energy_receive()
1551+
123.456
1552+
```
1553+
1554+
Does this deserve a Nobel prize?
1555+
1556+
#### 💡 Explanation:
1557+
1558+
* Notice that the numpy array created in the `energy_send` function is not returned, so that memory space is free to reallocate.
1559+
* `numpy.empty()` returns the next free memory slot without reinitializing it. This memory spot just happens to be the same one that was just freed (usually, but not always).
1560+
1561+
---
1562+
15331563
### yielding None
15341564
15351565
Suggested by @chris-rands in [this](https://github.com/satwikkansal/wtfpython/issues/32) issue.

0 commit comments

Comments
 (0)