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
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
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
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
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
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
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..
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(
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
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
10 matches
Mail list logo