[Python-ideas] Delayed Execution via Keyword
Pavol Lisy
pavol.lisy at gmail.com
Sat Feb 18 08:59:17 EST 2017
On 2/18/17, Steven D'Aprano <steve at pearwood.info> wrote:
Sorry Steve that I use your words probably too much out of context!
I just want to reuse your examples to analyze if proposed "delayed
execution" is really necessary. Thanks for them! :)
> print("Start")
> result = delayed: get_nth_prime(10**6) # I dislike this syntax
> print("Done")
> print(result)
If we change "delayed" keyword to "lambda" keyword then we just need
to explicitly say when to evaluate (which I am not sure is bad thing).
On 2/18/17, Steven D'Aprano <steve at pearwood.info> wrote:
> The caching means that:
> spam = delayed: calculate(1)
> eggs = spam
>
> eggs == spam would be true, and calculate would have only been called once,
> not twice.
If we really need something like this then we could use "def" keyword
(for delayed execution) and explicitely caching (for example with
functools.lru_cache).
print("Start")
@functools.lru_cache(1)
def result():
return get_nth_prime(10**6) # this code is delayed
print("Done")
print(result())
print("Start 2")
print(result())
result2 = result
print(result() == result2())
print("Done 2")
More information about the Python-ideas
mailing list