Re: proc A def/calls proc B: variable scoping rules.

2006-08-15 Thread NevilleDNZ
Steven D'Aprano wrote: > Basically, when you access a variable name on the left hand side of an > assignment (e.g. "a = 1") ANYWHERE in a function, that name is local to > that function UNLESS it has been declared global. ThanX Steven, I am still getting used to python scoping rules. I didn't real

Re: proc A def/calls proc B: variable scoping rules.

2006-08-15 Thread Steven D'Aprano
On Tue, 15 Aug 2006 20:28:34 -0700, NevilleDNZ wrote: > > Steve Holden wrote: >> No. It's too horrible to contemplate without getting mild feelings of >> nausea. What exactly is it you are tring to achieve here (since I assume >> your goal wasn't to make me feel sick :-)? > > It is part of an al

Re: proc A def/calls proc B: variable scoping rules.

2006-08-15 Thread Steven D'Aprano
On Tue, 15 Aug 2006 18:38:49 -0700, NevilleDNZ wrote: > UnboundLocalError: local variable 'x2' referenced before assignment > > I guess it is something to do with the scoping of duck typing. Er, no. Scoping and duck typing are completely different concepts. Scoping refers to the idea of where a

Re: proc A def/calls proc B: variable scoping rules.

2006-08-15 Thread NevilleDNZ
Steve Holden wrote: > No. It's too horrible to contemplate without getting mild feelings of > nausea. What exactly is it you are tring to achieve here (since I assume > your goal wasn't to make me feel sick :-)? It is part of an algorithum: #!/usr/bin/env python def A(k, x1, x2, x3, x4, x5): de

Re: proc A def/calls proc B: variable scoping rules.

2006-08-15 Thread Steve Holden
NevilleDNZ wrote: > I inserted x1,x2 into A to force a wider scope and it works. > > #!/usr/bin/env python > def A(): > print "begin A:" > A.x1=123; > A.x2=456; > def B(): > print "begin B:",A.x1,A.x2 > A.x2 = A.x2 + 210; # problem gone. > print "end B:",A.x1,A.x2 > print "pr

Re: proc A def/calls proc B: variable scoping rules.

2006-08-15 Thread NevilleDNZ
I inserted x1,x2 into A to force a wider scope and it works. #!/usr/bin/env python def A(): print "begin A:" A.x1=123; A.x2=456; def B(): print "begin B:",A.x1,A.x2 A.x2 = A.x2 + 210; # problem gone. print "end B:",A.x1,A.x2 print "pre B:",A.x1,A.x2 B() print "end A:",A.x

Re: proc A def/calls proc B: variable scoping rules.

2006-08-15 Thread Steve Holden
NevilleDNZ wrote: > Steve Holden wrote: > >>Hardly surprising. This statement is an assignment to x2, which >>therefore becomes local to the function. Since no previous value has >>been assigned to this local, the exception occurs. > > > But: In this case the assignment is never reached eg..

Re: proc A def/calls proc B: variable scoping rules.

2006-08-15 Thread NevilleDNZ
Steve Holden wrote: > Hardly surprising. This statement is an assignment to x2, which > therefore becomes local to the function. Since no previous value has > been assigned to this local, the exception occurs. But: In this case the assignment is never reached eg.. #!/usr/bin/env python def A(

Re: proc A def/calls proc B: variable scoping rules.

2006-08-15 Thread Steve Holden
NevilleDNZ wrote: > Can anyone explain why "begin B: 123" prints, but 456 doesn't? > > $ /usr/bin/python2.3 x1x2.py > begin A: > Pre B: 123 456 > begin B: 123 > Traceback (most recent call last): > File "x1x2.py", line 13, in ? > A() > File "x1x2.py", line 11, in A > B() > File "x1x

proc A def/calls proc B: variable scoping rules.

2006-08-15 Thread NevilleDNZ
Can anyone explain why "begin B: 123" prints, but 456 doesn't? $ /usr/bin/python2.3 x1x2.py begin A: Pre B: 123 456 begin B: 123 Traceback (most recent call last): File "x1x2.py", line 13, in ? A() File "x1x2.py", line 11, in A B() File "x1x2.py", line 7, in B print "begin B:",x