Antoon Pardon wrote:
Op 2005-03-16, Jeff Shannon schreef <[EMAIL PROTECTED]>:

Bruno Desthuilliers wrote:

- if x is a class attribute of class A and a is an instance of A, a.x=anyvalue create a new instance attribute x instead of modifying A.x

This is very consistent with the way that binding a name in any scope will shadow any bindings of that name in "higher" scopes. It is the same principle by which one is able to use the same name for a function-local variable that is used for a global variable, without destroying that global variable. [...]

Not entirely. The equivallent is imposible in function scope.
If function scope would work exactly equivallent as the
above the following should work


    a = 42
    def f():
      a = a + 1
      print a
    print a

And the result should be:

43
42


I'd still say that the name binding rules are very consistent. The name lookup rules are a little different (as they *should* be for class/instance attributes), and that's why there's a different net effect (UnboundLocalError) as shown in your example. I'd say, however, that if there's a special case here it's with the function-local variables, not the class/instance attributes. It's the optimizations to the function-local namespace which prevent transparent re-binding of global names. And given that the function-local namespace is by far the most heavily used, and the relative utility (and wisdom) of using globals in this way, this is a case where the benefit of the special case is well worth the cost of its slight inconsistency.


Jeff Shannon

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

Reply via email to