Class Variable Access and Assignment
steve at holdenweb.com
Fri Nov 4 05:53:28 CET 2005
> Thanks to all of you for your reply's i had no idea it would get this
> sort of response,
> i've read through most of the posts and it seems that its a active
> My question remains however, i suppose i'm not familiar with how this
> functions in
> other languages, but what is the common way of referring to a class
> is <class>.<var> the norm?
> or <instance>.<var> the norm.
Let me try to give a simple answer to a simple question before you begin
to feel that we are all barmy and maybe Perl would be an easier
Under normal circumstances, yes, if you want to reference and/or modify
a value that's intended to be shared amongst all instances of a given
class you would normally refer to that as
and such a reference will work inside any of the class's methods.
Inside the class body (but outside any method body) the same reference
can be written as
> I just seems to me that <instance>.<var> shouldn't defer to the class
> variable if
> an instance variable of the same name does not exists, it should, at
> least how i
> understand it raise an exception.
> Is my thinking way off here?
It's a long way from how Python is designed, because name resolution in
Python will look in an instance's class (and then the class's
superclass, and so on until object is reached, which is at the top of
all modern class hierarchies) unless the name is first found in the
That's just the way Python was designed. Like it or loathe it, it's too
late to change now. <shrug>. Some languages only use such a technique
for resolving the names associated with methods, but this isn't
pragmatical-ly y'rs - steve
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/
More information about the Python-list