Op 2005-11-03, Stefan Arentz schreef <[EMAIL PROTECTED]>: > Antoon Pardon <[EMAIL PROTECTED]> writes: > >> Op 2005-11-03, venk schreef <[EMAIL PROTECTED]>: >> > You see, >> > The seen behavior is due to the result of python's name >> > binding,scoping scheme. >> >> I know what causes the behaviour. But I still think it is >> not sane behaviour. >> >> >> > ... >> > >> > 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. >> >> This is an explanation depending on a specific implementation. >> >> Now can you give me a language design argument that supports the >> idea that in "b.a = b.a + 2" b.a refers to two different objects. >> >> And even if you could do that, can you give such an argument that >> in "b.a += 2" that one occurence of b.a should refer to two different >> objects. > > Your problem is a namespace conflict together with a certain > stubborness about lookup order :-) > > It is really simple. When you say b.a then the instance variable 'a' > is looked up first. If it does not exist then a class variable lookup > is done.
Fine, we have the code: b.a += 2 We found the class variable, because there is no instance variable, then why is the class variable not incremented by two now? > Remember, Python is a dynamic language. So? Python being a dynamic language doesn't prevent the following to fail: >>> a=1 >>> def f(): ... a += 2 ... >>> f() Traceback (most recent call last): File "<stdin>", line 1, in ? File "<stdin>", line 2, in f UnboundLocalError: local variable 'a' referenced before assignment > It is all according to how things have been in Python for a long time. Unsane behaviour for a long time is still unsane behaviour. > The real issue here is that you should propery name class variables so > that there can't be any confusion about class or instance scope. I use > all uppercase identifiers for class variables for example. The fact that this can be regarded as unwise coding, doesn't imply it is sane behaviour of python. Variable shadowing happens. I don't consider it sane behaviour if the same reference in a line gets resolved in different name spaces -- Antoon Pardon -- http://mail.python.org/mailman/listinfo/python-list