Class variables static by default?

Daniel Fetchinson fetchinson at googlemail.com
Mon Dec 21 09:03:03 EST 2009


>>> In python, 'class variable' is a variable that belongs to a class; not
>>> to the instance and is shared by all instance that belong to the class.
>>
>> Surely, since string variables are strings, and float variables are
>> floats, and bool variables are bools, and module variables are modules, a
>> class variable will be a class and an instance variable will be an
>> instance?
>>
>>> In contrast, 'instance variable' belongs to the instance, and each
>>> instance can make their instance variables refers to different objects
>>> than the other instances.
>>
>> The usual term used in Python is "class attribute" and "instance
>> attribute" for the named fields of a class or instance.
>
> I agree with your interpretation of "class variable", but you'll have to
> rewrite parts of the official Python documentation so it becomes
> consistent with it. The phrase "class variable" appears about 30 times,
> always meaning "class attribute"; four of them in the Language Reference,
> section "Class definitions", where the OP's issue is discussed:
>
> "Programmer’s note: Variables defined in the class definition are class
> variables; they are shared by all instances. To create instance variables,
> they can be set in a method with self.name = value. Both class and
> instance variables are accessible through the notation “self.name“, and an
> instance variable hides a class variable with the same name when accessed
> in this way. Class variables can be used as defaults for instance
> variables, but using mutable values there can lead to unexpected results.
> For new-style classes, descriptors can be used to create instance
> variables with different implementation details."

I don't think Steven cares much, he loves this type of nitpicking and
uber pedantic formulations, but only if he can apply it to other
people's post :)

I found that his posts are generally useful and helpful, one just has
to cut all the nitpicking, an example of which is the one you
responded to, and then everything is fine.

Duck and run.....
Daniel

-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown



More information about the Python-list mailing list