TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Show HN: Lazy evaluation in Python

88 pointsby joejevabout 10 years ago

7 comments

gamegoblinabout 10 years ago
Somewhat related -- A few months ago I wrote a decorator that can allow for function currying [0], like so:<p><pre><code> @curry def add3(a,b,c): return a+b+c # normal function application &gt;&gt;&gt; add3(1,2,3) 6 # add3(1,2) returns a unary function which is then applied to 3 &gt;&gt;&gt; add3(1,2)(3) 6 # rebinding partially applied functions to another variable, then applying &gt;&gt;&gt; add2 = add3(100) &gt;&gt;&gt; add2(5,6) 111 &gt;&gt;&gt; add1 = add2(3.14) &gt;&gt;&gt; add1(5) 108.14 &gt;&gt;&gt; map(add1,range(5)) [103.14, 104.14, 105.14, 106.14, 107.14] </code></pre> [0] <a href="https://gist.github.com/grantslatton/9221084" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;grantslatton&#x2F;9221084</a>
评论 #9134324 未加载
ssanderson11235about 10 years ago
This is really cool. The AST transformation stuff here is neat, but relatively well-trodden ground.<p>The more impressive new science here is the lazy_function decorator, which is implemented as a bytecode transformer on the code object that lives inside the decorated function. The author built his own library for the bytecode stuff, which lives here: <a href="https://github.com/llllllllll/codetransformer" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;llllllllll&#x2F;codetransformer</a>.
dagssabout 10 years ago
You said in a comment that you&#x27;re looking for a usecase for this technique, so I&#x27;ll provide one for something similar, perhaps we&#x27;ll get ideas.<p>I&#x27;ve been toying with something similar lately, as a caching framework for scientific computations. I will have something like:<p><pre><code> x = load_big_file(filename) # takes 2 minutes y = sqrt(1 &#x2F; x ** 2) # takes 4 seconds ... </code></pre> Then, as my work proceed, I will change and tweak and re-run in the same process...clearly a lot of my time would have been saved by caching (though a different part each time depending on what I tweak).<p>The way to go currently is use joblib, which provides a decorator to put on a function to do basic caching. However you have to take care to manually clear cache if a function you&#x27;re dependent on changes, or sometimes it will clear cache itself because you changed something irrelevant to the computation.<p>So the lazy alternative idea I had is a lazily evaluated tree similar to this, where the purpose is looking up a cache using the AST as key. What I have now looks more like this:<p><pre><code> @pure def load_big_file(filename): ... &gt;&gt;&gt; x = load_big_file(lazy(fileref(filename))) # fileref is like a string but hashes by timestamp of file.. &gt;&gt;&gt; y = sqrt(1 &#x2F; x ** 2) &gt;&gt;&gt; print y &lt;lazy 23wfas input: v1: 32rwaa &quot;&#x2F;home&#x2F;dagss&#x2F;data&#x2F;myfile.dat&quot; @ 2015-03-03 08:43:23 program: e0: 43wafa v1**2 e1: 4rfafq 1 &#x2F; e0 e2: sqrt(e1) &gt; </code></pre> The point is every node in the syntax tree gives a hash (leafs having their value hashed, inner nodes having a hash based on the operation and the inputs). Then implementing a cache is simply<p><pre><code> NULL = object() y = cache.get(y, NULL) if y is NULL: y = cache[y] = compute(y) </code></pre> I&#x27;m leaning a bit towards explicit being better than implicit though (having to call compute(y) rather than it happening when you need it to be evaluated transparently..)
评论 #9138642 未加载
one-more-minuteabout 10 years ago
Are there any interesting use-cases for this and&#x2F;or problems it solves? Not that there necessarily have to be, of course, I&#x27;m just curious.
评论 #9133704 未加载
apenguinabout 10 years ago
Do you actually intend on writing a description later, or is that just a joke?
评论 #9133709 未加载
chaokyabout 10 years ago
Interesting, very reminiscent of Lisp macros! Glad to see that python code transforming isn&#x27;t too difficut.
评论 #9134518 未加载
zbowlingabout 10 years ago
your github username is frustrating.
评论 #9133474 未加载