Closures in leu of pointers?

Fábio Santos fabiosantosart at gmail.com
Sat Jun 29 05:42:50 EDT 2013


On 29 Jun 2013 10:38, <cts.private.yahoo at gmail.com> wrote:
>
> Hi,
>
> I'd like to use closures to set allow a subroutine to set variables in
its caller, in leu of pointers.  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?

The fact that msg3 is assigned to in that scope makes it a local variable.
It doesn't matter if the assignment happens later. Python will treat it as
local, and so won't look for it outside the local scope/closure.

The fix is to declare msg3 as global, I think.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130629/fb3149e3/attachment.html>


More information about the Python-list mailing list