Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #55699
| From | Hrvoje Niksic <hniksic@xemacs.org> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Parameterized functions of no arguments? |
| Date | 2011-02-11 15:43 +0100 |
| Organization | B.net Hrvatska d.o.o. |
| Message-ID | <87bp2izl6a.fsf@xemacs.org> (permalink) |
| References | <ij2fdr$uo7$1@news.eternal-september.org> <mailman.91.1297403413.1633.python-list@python.org> |
Chris Rebert <clp2@rebertia.com> writes: > It's a well-known problem due to the intricacies of Python's scoping > rules. Actually, it is not specific to Python's scoping rules (which mandate local, then module-level, then built-in name lookup). The root of the surprise is, as you correctly point out, the fact that the variable's "cell" is shared by all iterations through the loop. Taking that into account, it logically follows that all enclosed functions end up reading the same value. This is not endemic to Python, the exact same surprise is present in Common Lisp, a language with long tradition of closures and otherwise radically different scoping rules. * (setq l (loop for i from 1 to 10 collect (lambda () i))) * (mapcar #'funcall l) (11 11 11 11 11 11 11 11 11 11)
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Parameterized functions of no arguments? Rotwang <sg552@hotmail.co.uk> - 2011-02-11 04:54 +0000 Re: Parameterized functions of no arguments? Hrvoje Niksic <hniksic@xemacs.org> - 2011-02-11 15:43 +0100 Re: Parameterized functions of no arguments? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2011-02-10 23:22 -0800 Re: Parameterized functions of no arguments? Carl Banks <pavlovevidence@gmail.com> - 2011-02-11 05:59 +0000 Re: Parameterized functions of no arguments? Chris Rebert <clp2@rebertia.com> - 2011-02-10 21:50 -0800 Re: Parameterized functions of no arguments? Rotwang <sg552@hotmail.co.uk> - 2011-02-11 05:26 +0000
csiph-web