Closures in leu of pointers?
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? -- http://mail.python.org/mailman/listinfo/python-list
Re: Closures in leu of pointers?
Well, it would have been French if I had spelled it right - since you force me overcome my laziness, I see I should have spelled it lieu ... Thank you. You reminded me of the (weak) workaround of using arrays and confirmed my suspicion that I although I can read the variable, I won't be able to write to it. I still don't understand why not, though... As for python 3 ... "nonlocal"? I see I'm not alone in picking obnoxious names ... -- http://mail.python.org/mailman/listinfo/python-list
Re: Closures in leu of pointers?
Alas, one reason it's a weak workaround is that it doesn't work - at least, not how I wish it would: $ cat ptrs x = 34 def p1 (a1): a1[0] += 12 p1 ([x]) print (x) $ python ptrs 34 -- http://mail.python.org/mailman/listinfo/python-list
Re: Closures in leu of pointers?
"PS: If you're reading this and love the French language -- I am deeply sorry for the pain I'm causing you..." It's obviously a team effort... My French ain't so hot, either. I had to google your "tout chez" until I ran into the explanation: hallo :) also ich gucke super gerne two and a half men und da wird öfters tout chez (keine ahnung ob es so geschrieben wird) gesagt. ich hab gegooglet aber nichts gefunden. ich habs auch überstzen lassen aber da kommt nur raus "alles bei"...das wirds ja wohl nicht sein^^ könnte mir also jemand sagen was es genau bedeutet wenn man das sagt? The answer for us TV-challenged non-views: Es heißt: "touché" -- http://mail.python.org/mailman/listinfo/python-list
Re: Closures in leu of pointers?
I love the title. Reminds me of Ivanhoe ... great time travel. -- http://mail.python.org/mailman/listinfo/python-list
Re: Closures in leu of pointers?
:) Thank you guys for saying what I was biting my tongue about (thanks everybody for the help, BTW!). This "python-think" stuff was starting to get on my nerves - but then it occurred to me that - although having many powerful features - it has so many weird restrictions that it requires a special way of thinking and problem solving. I have to work with perl's object-orientation stuff again for awhile, in order to see if either has an advantage. -- http://mail.python.org/mailman/listinfo/python-list
Re: Closures in leu of pointers?
exactly that. Without wanting to analyze it in too much depth now, I would want a local keyword to allow me to know I was protecting my variables, and a way to specify other scopes, without so much implied scoping in non-intuitive ways... Now everybody is gonna tell me how wrong I am, but you asked what I want, and that's what keeps aggrevating me with python. -- http://mail.python.org/mailman/listinfo/python-list
Re: Closures in leu of pointers?
Touchy aren't we ... :) -- http://mail.python.org/mailman/listinfo/python-list
Re: Closures in leu of pointers?
No, actually, it's okay that it's local by default, after all. TCL's got that capability of explicitly specifying the scope (up n or something like that?). That's okay for tcl, not sure if it would seem so elegant for python. But you can't tell me that the scenarios that I presented in the beginning of this thread are intuitive. I'm going to have a very hard time ever accepting a language that lets me read a variable but when I try to write to it, it suddenly doesn't want to have to do with it anymore ... but hey, like somebody said - everybody's got their own sense of 'plictedness. -- http://mail.python.org/mailman/listinfo/python-list