Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'argument': 0.05; 'assignment': 0.07; 'variables': 0.07; 'global,': 0.09; 'referenced': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'before!': 0.16; 'closures': 0.16; 'pointers.': 0.16; 'proc': 0.16; 'subroutine': 0.16; 'think.': 0.16; 'variable.': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'variable': 0.18; 'module': 0.19; 'seems': 0.21; 'import': 0.22; 'email addr:gmail.com>': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'script': 0.25; '>': 0.26; 'skip:" 40': 0.26; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; 'work.': 0.31; '13,': 0.31; 'anyone': 0.31; 'file': 0.32; 'agreed': 0.32; 'me?': 0.32; 'worked': 0.33; '(most': 0.33; "i'd": 0.34; "can't": 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'words,': 0.36; 'subject:?': 0.36; 'hi,': 0.36; 'expected': 0.38; 'remote': 0.38; 'skip:& 10': 0.38; 'follows:': 0.38; 'fact': 0.38; 'recent': 0.39; 'explain': 0.39; 'skip:& 20': 0.39; 'skip:u 10': 0.60; 'new': 0.61; 'matter': 0.61; 'more': 0.64; 'to:addr:gmail.com': 0.65; 'huh?': 0.84; 'local,': 0.84; 'sudden,': 0.84; '\xa0but': 0.84; '\xa0hello': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=i/PHGPflSQ43WQbH+bGNIMvepNBmYAOq2IXVkSo1DrM=; b=Fw0m+Svy3Cw7cNqXNZ+kEp9Jj178H3BQ0se8udAL0JO9LJiIg0L+9UkOJZnblu6cc3 eVeDOviZA4InqLs14L49CkI2sW04A5faatldHTEZHTluOixydM5rtNecRQIn/PrExEwJ v8HaAANmafW7ODdNtK1Hki5oDAztkjgxtz1mGdGlPGzq3T1QAZpQrNScQAoQBb7dVslb X3Vwgy3dSRGHhLpVZGzi/8OLhtvUKRd81LjlCbtmrD3gH9ojIKl7lpVN8In7YwrZb0/d LCQB8IJ1tnp7ePNHhRcZDLesECaN5IDM5C0JznI+AOJRoKvTBQZW2a/G4GkhG8IlUjCa Udjw== MIME-Version: 1.0 X-Received: by 10.49.16.233 with SMTP id j9mr21958226qed.12.1372498970485; Sat, 29 Jun 2013 02:42:50 -0700 (PDT) In-Reply-To: <2a2072e3-4b12-4ada-872c-1240d2379928@googlegroups.com> References: <2a2072e3-4b12-4ada-872c-1240d2379928@googlegroups.com> Date: Sat, 29 Jun 2013 10:42:50 +0100 Subject: Re: Closures in leu of pointers? From: =?ISO-8859-1?Q?F=E1bio_Santos?= To: cts.private.yahoo@gmail.com Content-Type: multipart/alternative; boundary=047d7bea38e2972a3a04e047d1a8 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 186 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1372499409 news.xs4all.nl 15916 [2001:888:2000:d::a6]:51081 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:49409 --047d7bea38e2972a3a04e047d1a8 Content-Type: text/plain; charset=ISO-8859-1 On 29 Jun 2013 10:38, 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 > 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. --047d7bea38e2972a3a04e047d1a8 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable


On 29 Jun 2013 10:38, <ct= s.private.yahoo@gmail.com> wrote:
>
> Hi,
>
> I'd like to use closures to set allow a subroutine to set variable= s in its caller, in leu of pointers. =A0But I can't get it to work. =A0= I have the following test pgm, but I can't understand its behaviour: >
> It uses a function p2() from the module modules.closure1b:
>
> =A0 def p2 (proc):
> =A0 =A0 proc ("dolly")
>
> I thought the following worked like I expected it to:
>
>
> from modules.closures1b import p2
>
> def p1(msg1):
> =A0 =A0 msg3 =3D "world"
> =A0 =A0 print "p1: entered: ", msg1
> =A0 =A0 def p11(msg2):
> =A0 =A0 =A0 =A0 print "p11: entered: ", msg2
> =A0 =A0 =A0 =A0 print msg1 + msg2 + msg3
> =A0 =A0 print p2 (p11)
>
> p1('hello')
>
> $ python closures1c.py
> p1: entered: =A0hello
> p11: entered: =A0dolly
> 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 mod= ule and it can access not only its own argument (msg2) but also the variabl= es 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:
> =A0 =A0 =A0 =A0 msg3 =3D "goodbye"
> as follows:
>
> from modules.closures1b import p2
>
> def p1(msg1):
> =A0 =A0 msg3 =3D "world"
> =A0 =A0 print "p1: entered: ", msg1
> =A0 =A0 def p11(msg2):
> =A0 =A0 =A0 =A0 print "p11: entered: ", msg2
> =A0 =A0 =A0 =A0 print msg1 + msg2 + msg3
> =A0 =A0 =A0 =A0 msg3 =3D "goodbye" =A0 =A0 =A0 =A0 =A0# <= - new
> =A0 =A0 print p2 (p11)
>
> p1('hello')
>
> then all of a sudden, I get this:
>
> p1: entered: =A0hello
> p11: entered: =A0dolly
> Traceback (most recent call last):
> =A0 File "closures1c.py", line 13, in <module>
> =A0 =A0 p1('hello')
> =A0 File "closures1c.py", line 11, in p1
> =A0 =A0 print p2 (p11)
> =A0 File "/home/mellman/eg/python/modules/closures1b.py", li= ne 2, in p2
> =A0 =A0 proc ("dolly")
> =A0 File "closures1c.py", line 9, in p11
> =A0 =A0 print msg1 + msg2 + msg3
> UnboundLocalError: local variable 'msg3' referenced before ass= ignment
>
>
> Huh? =A0msg3 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 l= ocal variable. It doesn't matter if the assignment happens later. Pytho= n will treat it as local, and so won't look for it outside the local sc= ope/closure.

The fix is to declare msg3 as global, I think.

--047d7bea38e2972a3a04e047d1a8--