You see,
     The seen behavior is due to the result of python's name
binding,scoping scheme.
    Let me give you an example,
 class A:
     i=0
     def t(self):
             print self.i
             self.i=4
then
a=A()
a.i is 0
a.t()
then,
A.i is 0
a.i is 4

In the function, it first searches for i in its local scope, on not
finding it, accesses the class object's i.
then the next line, is an assignment, which binds (creates a new
variable) in the instance's scope. you can use the buit-in id function
to verify it.

the same thing happens in the case of b.a = b.a + 2 .... search for b.a
not found, read the value from the enclosing scope (of the class
object).... then assign b.a to the local scope, with the value 3.

But, I think your question about the sanity of the behaviour should be
analysed sincerely....

if,
k=0
def f():
   print k
   k=k+1
raises UnboundLocalError, then how is it accepted in the former case?
hmmm....

maybe, my arguments are hapazard.... but, i'll get to know when i'm
flamed ;)

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to