Class-level variables - a scoping issue

Lawrence D'Oliveiro ldo at geek-central.gen.new_zealand
Sun Oct 10 03:53:27 EDT 2010


In message <4cb14f8c$0$1627$742ec2ed at news.sonic.net>, John Nagle wrote:

> Within "fn1", the first reference to "self.classvar" references the class-
> level version of "classvar".  The assignment overrides that and creates an
> object-level instance of "self.classvar". Further references to
> self.classvar" in f1 then reference the object-level "classvar"

I’d say there is definitely an inconsistency with the absoutely anal 
restrictions on global variables, i.e. the well-known sequence

    ... reference to globalvar ...
    globalvar = newvalue

which triggers the error “UnboundLocalError: local variable 'globalvar' 
referenced before assignment”.

It seems to me the same principle, that of disallowing implicit overriding 
of a name from an outer scope with that from an inner one after the former 
has already been referenced, should be applied here as well.



More information about the Python-list mailing list