tag:blogger.com,1999:blog-4982215793068879977.post2451024088879892439..comments2023-09-07T02:56:05.540+08:00Comments on fcamel 技術隨手記: Python 的 Closurefcamelhttp://www.blogger.com/profile/04792244455260595133noreply@blogger.comBlogger2125tag:blogger.com,1999:blog-4982215793068879977.post-90946572663620394942010-02-20T17:32:57.499+08:002010-02-20T17:32:57.499+08:00用 closure @.@
(def a
(let [a (atom 0)]
#(sw...用 closure @.@<br /><br />(def a<br /> (let [a (atom 0)]<br /> #(swap! a inc)))<br /><br />(a) ; 傳回1<br />(a) ; 傳回2<br />(a) ; 傳回3Plummhttps://www.blogger.com/profile/01079833530407761100noreply@blogger.comtag:blogger.com,1999:blog-4982215793068879977.post-50963836425103440292010-02-20T00:35:46.341+08:002010-02-20T00:35:46.341+08:00PEP 3104 (http://www.python.org/dev/peps/pep-3104/...PEP 3104 (http://www.python.org/dev/peps/pep-3104/#new-syntax-in-the-binding-outer-scope), already implemented in python3, introduced the new 'nonlocal' keyword to allow modifying a variable in an outer scope.<br />Thus the 'weirdness' regarding Python's closures is a thing of the past.<br /><br />When pass first class functions around as data, having closures is often more convenient then having to implement __call__() on a class, no?Anonymoushttps://www.blogger.com/profile/18195653023013629876noreply@blogger.com