Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #49410
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'argument': 0.05; 'explicitly': 0.05; '(python': 0.07; 'assignment': 0.07; 'statically': 0.07; 'variables': 0.07; '[0]': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'referenced': 0.09; 'python': 0.11; 'def': 0.12; "'n'": 0.16; '2):': 0.16; 'before!': 0.16; 'closures': 0.16; 'declaration': 0.16; 'determines': 0.16; 'mutable': 0.16; 'pointers.': 0.16; 'proc': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subroutine': 0.16; 'variable.': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'module': 0.19; 'seems': 0.21; '>>>': 0.22; 'import': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'script': 0.25; 'skip:" 40': 0.26; 'this:': 0.26; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'work.': 0.31; '"",': 0.31; '13,': 0.31; 'anyone': 0.31; 'file': 0.32; 'agreed': 0.32; 'me?': 0.32; 'worked': 0.33; '(most': 0.33; 'becomes': 0.33; 'actual': 0.34; "i'd": 0.34; 'problem': 0.35; "can't": 0.35; 'test': 0.35; 'but': 0.35; 'add': 0.35; 'words,': 0.36; 'subject:?': 0.36; 'expected': 0.38; 'remote': 0.38; 'follows:': 0.38; 'to:addr:python-list': 0.38; 'recent': 0.39; 'explain': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:u 10': 0.60; 'most': 0.60; 'email addr:gmail.com': 0.63; 'name': 0.63; 'more': 0.64; 'huh?': 0.84; 'sudden,': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: Closures in leu of pointers? |
| Date | Sat, 29 Jun 2013 12:01:47 +0200 |
| Organization | None |
| References | <2a2072e3-4b12-4ada-872c-1240d2379928@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p508485e6.dip0.t-ipconnect.de |
| User-Agent | KNode/4.7.3 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3984.1372500107.3114.python-list@python.org> (permalink) |
| Lines | 136 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1372500107 news.xs4all.nl 15890 [2001:888:2000:d::a6]:56506 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:49410 |
Show key headers only | View raw
cts.private.yahoo@gmail.com wrote:
> I'd like to use closures to set allow a subroutine to set variables in its
> caller, in leu of pointers.
"leu"? Must be a Fench word ;)
> But I can't get it to work. I have the
> following test pgm, but I can't understand its behaviour:
>
> It uses a function p2() from the module modules.closure1b:
>
> def p2 (proc):
> proc ("dolly")
>
> I thought the following worked like I expected it to:
>
>
> from modules.closures1b import p2
>
> def p1(msg1):
> msg3 = "world"
> print "p1: entered: ", msg1
> def p11(msg2):
> print "p11: entered: ", msg2
> print msg1 + msg2 + msg3
> print p2 (p11)
>
> p1('hello')
>
> $ python closures1c.py
> p1: entered: hello
> p11: entered: dolly
> hellodollyworld
> None
>
> In other words, p1() is passed "hello" for msg1, "world" goes to the local
> msg3 and then p11() is invoked out of a remote module and it can access
> not only its own argument (msg2) but also the variables local to p1():
> "hellodollyworld".
>
> But if I try to set the variable local to p1(), all of a sudden python
> seems to forget everything we agreed on.
>
> If I add this line to the script above:
> msg3 = "goodbye"
> as follows:
>
> from modules.closures1b import p2
>
> def p1(msg1):
> msg3 = "world"
> print "p1: entered: ", msg1
> def p11(msg2):
> print "p11: entered: ", msg2
> print msg1 + msg2 + msg3
> msg3 = "goodbye" # <- new
> print p2 (p11)
>
> p1('hello')
>
> then all of a sudden, I get this:
>
> p1: entered: hello
> p11: entered: dolly
> Traceback (most recent call last):
> File "closures1c.py", line 13, in <module>
> p1('hello')
> File "closures1c.py", line 11, in p1
> print p2 (p11)
> File "/home/mellman/eg/python/modules/closures1b.py", line 2, in p2
> proc ("dolly")
> File "closures1c.py", line 9, in p11
> print msg1 + msg2 + msg3
> UnboundLocalError: local variable 'msg3' referenced before assignment
>
>
> Huh? msg3 isn't more referenced than it was before!
>
> Can anyone explain this to me?
You picked the most obnoxious variable names I can think of, but the actual
problem is simple:
Python statically determines the scope of a variable. If you rebind a name
it becomes a local variable unless you explicitly declare it as global or --
in Python 3 -- as nonlocal. For example:
Wrong:
>>> def outer():
... n = 0
... def inner():
... print(n)
... n += 1
... return inner
...
>>> outer()()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in inner
UnboundLocalError: local variable 'n' referenced before assignment
With nonlocal declaration (Python 3 only):
>>> def outer():
... n = 0
... def inner():
... nonlocal n
... print(n)
... n += 1
... return inner
...
>>> f = outer()
>>> f()
0
>>> f()
1
>>> f()
2
With a mutable variable as a pseudo-namespace (workaround for Python 2):
>>> def outer():
... n = [0]
... def inner():
... print n[0]
... n[0] += 1
... return inner
...
>>> f = outer()
>>> f()
0
>>> f()
1
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Closures in leu of pointers? cts.private.yahoo@gmail.com - 2013-06-29 02:34 -0700
Re: Closures in leu of pointers? Fábio Santos <fabiosantosart@gmail.com> - 2013-06-29 10:42 +0100
Re: Closures in leu of pointers? Peter Otten <__peter__@web.de> - 2013-06-29 12:01 +0200
Re: Closures in leu of pointers? cts.private.yahoo@gmail.com - 2013-06-29 04:21 -0700
Re: Closures in leu of pointers? cts.private.yahoo@gmail.com - 2013-06-29 04:44 -0700
Re: Closures in leu of pointers? Peter Otten <__peter__@web.de> - 2013-06-29 14:12 +0200
Re: Closures in leu of pointers? cts.private.yahoo@gmail.com - 2013-06-29 05:26 -0700
Re: Closures in leu of pointers? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-06-29 15:37 +0100
Re: Closures in leu of pointers? cts.private.yahoo@gmail.com - 2013-06-29 07:48 -0700
Re: Closures in leu of pointers? Michael Torrie <torriem@gmail.com> - 2013-06-29 07:56 -0600
Re: Closures in leu of pointers? Michael Torrie <torriem@gmail.com> - 2013-06-29 12:36 -0600
Re: Closures in leu of pointers? Michael Torrie <torriem@gmail.com> - 2013-06-29 08:02 -0600
Re: Closures in leu of pointers? Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-06-29 19:02 +0200
Re: Closures in leu of pointers? rusi <rustompmody@gmail.com> - 2013-06-29 10:33 -0700
Re: Closures in leu of pointers? cts.private.yahoo@gmail.com - 2013-06-29 11:37 -0700
Re: Closures in leu of pointers? Michael Torrie <torriem@gmail.com> - 2013-06-29 12:58 -0600
Re: Closures in leu of pointers? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-29 18:59 +0000
Re: Closures in leu of pointers? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-29 18:51 +0000
Re: Closures in leu of pointers? Michael Torrie <torriem@gmail.com> - 2013-06-29 13:04 -0600
Re: Closures in leu of pointers? rusi <rustompmody@gmail.com> - 2013-06-29 12:11 -0700
Re: Closures in leu of pointers? Michael Torrie <torriem@gmail.com> - 2013-06-29 12:35 -0600
Re: Closures in leu of pointers? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-29 19:19 +0000
Re: Closures in leu of pointers? Tim Chase <tim@thechases.com> - 2013-06-29 14:42 -0500
Re: Closures in leu of pointers? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-30 01:41 +0000
Re: Closures in leu of pointers? Joshua Landau <joshua.landau.ws@gmail.com> - 2013-06-29 21:02 +0100
Re: Closures in leu of pointers? Michael Torrie <torriem@gmail.com> - 2013-06-29 16:02 -0600
Re: Closures in leu of pointers? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-29 18:45 +0000
Re: Closures in leu of pointers? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-29 19:00 +0000
Re: Closures in leu of pointers? cts.private.yahoo@gmail.com - 2013-06-29 12:20 -0700
Re: Closures in leu of pointers? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-29 19:33 +0000
Re: Closures in leu of pointers? cts.private.yahoo@gmail.com - 2013-06-29 12:34 -0700
Re: Closures in leu of pointers? Ian Kelly <ian.g.kelly@gmail.com> - 2013-06-29 13:47 -0600
Re: Closures in leu of pointers? Terry Reedy <tjreedy@udel.edu> - 2013-06-29 16:53 -0400
Re: Closures in leu of pointers? rusi <rustompmody@gmail.com> - 2013-06-30 01:56 -0700
Re: Closures in leu of pointers? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-30 11:22 +0000
Re: Closures in leu of pointers? rusi <rustompmody@gmail.com> - 2013-06-30 04:42 -0700
Re: Closures in leu of pointers? Ian Kelly <ian.g.kelly@gmail.com> - 2013-06-29 15:21 -0600
Re: Closures in leu of pointers? cts.private.yahoo@gmail.com - 2013-06-29 14:30 -0700
Re: Closures in leu of pointers? Terry Reedy <tjreedy@udel.edu> - 2013-06-30 00:32 -0400
Re: Closures in leu of pointers? Chris Angelico <rosuav@gmail.com> - 2013-06-30 15:08 +1000
Re: Closures in leu of pointers? rusi <rustompmody@gmail.com> - 2013-06-30 00:36 -0700
Re: Closures in leu of pointers? Ian Kelly <ian.g.kelly@gmail.com> - 2013-06-29 23:46 -0600
Re: Closures in leu of pointers? alex23 <wuwei23@gmail.com> - 2013-07-01 14:57 +1000
Re: Closures in leu of pointers? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-01 07:36 +0000
Re: Closures in leu of pointers? Chris Angelico <rosuav@gmail.com> - 2013-06-30 15:59 +1000
Re: Closures in leu of pointers? Terry Reedy <tjreedy@udel.edu> - 2013-06-30 02:11 -0400
Re: Closures in leu of pointers? Ian Kelly <ian.g.kelly@gmail.com> - 2013-06-30 00:24 -0600
Re: Closures in leu of pointers? Michael Torrie <torriem@gmail.com> - 2013-06-29 16:03 -0600
Re: Closures in leu of pointers? Ian Kelly <ian.g.kelly@gmail.com> - 2013-06-29 13:23 -0600
Re: Closures in leu of pointers? Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-06-30 12:07 +0200
Re: Closures in leu of pointers? Ian Kelly <ian.g.kelly@gmail.com> - 2013-06-30 11:13 -0600
Re: Closures in leu of pointers? Ian Kelly <ian.g.kelly@gmail.com> - 2013-06-30 11:18 -0600
csiph-web