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




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():
print "begin A:"
x1=123
x2=456
def B():
print "begin B:",x1,x2
if False: x2 = x2 + 210 # Magically disappears when this line is
commented out.
print "end B:",x1,x2
print "pre B:",x1,x2
B()
print "end A:",x1,x2
A()

# same error message...
$ ./x1x2.py
begin A:
Pre B: 123 456
begin B: 123
Traceback (most recent call last):
File "./x1x2.py", line 13, in <module>
A()
File "./x1x2.py", line 11, in A
B()
File "./x1x2.py", line 7, in B
print "begin B:",x1,x2
UnboundLocalError: local variable 'x2' referenced before assignment

I guess it is something to do with the scoping of duck typing.

I WAS expecting that the A.x2 was visable until x2 is somehow (by
assignment) made local. I guess I am learning that what you do to a
variable in the middle of scope (even in an unreachable statement)
effects the entire scope. Is there anyway to force x2 to be A.x2
(without declaring it to be a global.x2)? Maybe I can put it into a
A.local class...

The result sh/could be:
begin A:
pre B: 123 456
begin B: 123 456
end B: 123 666
end A: 123 666

ThanX
NevilleD

.



Relevant Pages

  • Re: Try Catch Finally , pointers and Vb.net 2005 compiler warnings
    ... A null reference exception could result at runtime. ... The 'f' in front of 'IsNot' is underlined and the warning included as a comment in the snippet above is shown. ... It leads people to write explicit assignments, which is not necessary because the compiler adds the assignment automatically. ... possible, but not always the best solution (minimum scope is often the best choice, and in this particular case it's the same scope). ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Nested scopes, and augmented assignment
    ... nested scope, on variables from the outer scope: ... def f: ... leftside of an assignment should depend on whether the name ... is the target or prefix for the target. ...
    (comp.lang.python)
  • Re: Nested scopes, and augmented assignment
    ... AP> rebind variables that are out of the local scope. ... AP> side of an assignment is a simple name it will only search in the ... AP> rebinding a name in an intermediate scope. ... AP> def f: ...
    (comp.lang.python)
  • Re: struct problems
    ... because my previous programming experience made the assignment before ... at least three programming languages by copy-pasteing. ... that you are ONLY in the scope of functions. ... GNU C and disregard the limitations of other compilers. ...
    (comp.lang.c)
  • Re: struct problems
    ... this distinction though some, helpfully, have more distinctive syntax. ... The only worlds in which it is an assignment are imaginary ones. ... call it initialise if you want. ... that you are ONLY in the scope of functions. ...
    (comp.lang.c)