mirror of
https://github.com/satwikkansal/wtfpython
synced 2024-11-25 04:24:23 +01:00
Add new example: Teleportation
This commit is contained in:
parent
118f51d042
commit
c84c827248
30
README.md
vendored
30
README.md
vendored
@ -1530,6 +1530,36 @@ tuple()
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
### Teleportation
|
||||||
|
|
||||||
|
Suggested in [this](https://www.reddit.com/r/Python/comments/6x6upn/wtfpython_a_collection_of_interesting_and_tricky/dme96dq/) reddit thread.
|
||||||
|
|
||||||
|
```py
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
def energy_send(x):
|
||||||
|
np.array([float(x)])
|
||||||
|
|
||||||
|
def energy_receive():
|
||||||
|
return np.empty((), dtype=np.float).tolist()
|
||||||
|
```
|
||||||
|
|
||||||
|
**Output:**
|
||||||
|
```py
|
||||||
|
>>> energy_send(123.456)
|
||||||
|
>>> energy_receive()
|
||||||
|
123.456
|
||||||
|
```
|
||||||
|
|
||||||
|
Does this deserve a Nobel prize?
|
||||||
|
|
||||||
|
#### 💡 Explanation:
|
||||||
|
|
||||||
|
* Notice that the numpy array created in the `energy_send` function is not returned, so that memory space is free to reallocate.
|
||||||
|
* `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).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
### yielding None
|
### yielding None
|
||||||
|
|
||||||
Suggested by @chris-rands in [this](https://github.com/satwikkansal/wtfpython/issues/32) issue.
|
Suggested by @chris-rands in [this](https://github.com/satwikkansal/wtfpython/issues/32) issue.
|
||||||
|
Loading…
Reference in New Issue
Block a user