Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Class variables static by default?

6 views
Skip to first unread message

KarlRixon

unread,
Dec 19, 2009, 7:10:13 PM12/19/09
to
Given the following script, I'd expect p1.items to just contain
["foo"] and p2.items to contain ["bar"] but they both contain ["foo",
"bar"].

Why is this? Are object variables not specific to their instance?

---------------------------
#!/usr/bin/env python

class Parser:
items = []
def add_item(self, item):
self.items.append(item)

p1 = Parser()
p1.add_item("foo")
p2 = Parser()
p2.add_item("bar")

print p1
print p2
print p1.items
print p2.items
----------------------------

Output:
<__main__.Parser instance at 0x7fd812ccc098>
<__main__.Parser instance at 0x7fd812ccc0e0>
['foo', 'bar']
['foo', 'bar']

John Posner

unread,
Dec 19, 2009, 7:29:57 PM12/19/09
to
On Sat, 19 Dec 2009 19:10:13 -0500, KarlRixon <karl...@gmail.com> wrote:

> Given the following script, I'd expect p1.items to just contain
> ["foo"] and p2.items to contain ["bar"] but they both contain ["foo",
> "bar"].
>
> Why is this? Are object variables not specific to their instance?
>
> ---------------------------
> #!/usr/bin/env python
>
> class Parser:
> items = []
> def add_item(self, item):
> self.items.append(item)
>

<snip>

You're using a *class attribute* instead of an *instance attribute*.
Change the class definition to:

class Parser:
def __init__(self):
self.items = []

def add_item(self, item):
self.items.append(item)

-John

Cameron Simpson

unread,
Dec 19, 2009, 7:44:08 PM12/19/09
to pytho...@python.org
On 19Dec2009 16:10, KarlRixon <karl...@gmail.com> wrote:
| Given the following script, I'd expect p1.items to just contain
| ["foo"] and p2.items to contain ["bar"] but they both contain ["foo",
| "bar"].
|
| Why is this? Are object variables not specific to their instance?

You haven't instatiated "items" in the object instance, so python finds
it further out in the class. One class, one variable; it looks "static".

Compare this (yours):

class Parser:
items = []
def add_item(self, item):
self.items.append(item)

with this:

class Parser:
def __init__(self):
self.items = []
def add_item(self, item):
self.items.append(item)

The first makes an "items" in the class namespace.

The second makes an "items" in each object as it is initialised.

Run the rest of your code as is and compare.

When you say "self.items" python looks first in the object and then in
the class. Your code didn't put it in the object namespace, so it found
the one in the class.

Cheers,
--
Cameron Simpson <c...@zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

I have always been a welly man myself. They are superb in wet grass, let
alone lagoons of pig shit. - Julian Macassey

Lie Ryan

unread,
Dec 19, 2009, 7:44:11 PM12/19/09
to
On 12/20/2009 11:10 AM, KarlRixon wrote:
> Given the following script, I'd expect p1.items to just contain
> ["foo"] and p2.items to contain ["bar"] but they both contain ["foo",
> "bar"].
>
> Why is this? Are object variables not specific to their instance?

First of all, dump all the preconception of what 'static' and 'class
variable' means in any previous language you've learned. Those terms is
not the same in python, and not even referring to a similar concept.

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.

In contrast, 'instance variable' belongs to the instance, and each
instance can make their instance variables refers to different objects
than the other instances.

Note that, in python, an object can be "owned" by multiple variables, or
more precisely "an object can have multiple names".

'static' in python refers to 'staticmethod', these are called
'classmethod' in other languages (C/C++/Java). Python's 'classmethod' is
an entirely different beast than 'classmethod' in other languages.

'staticmethod' in python is a method in a class that is not bound to the
class nor the instance; the class is merely used as organizational tool.
'classmethod' in python is a method that is bound to the class instead
of the instance; the first argument (self/cls) of a 'classmethod' is
bound to the class instead of the instance.

Steven D'Aprano

unread,
Dec 19, 2009, 11:16:16 PM12/19/09
to
On Sun, 20 Dec 2009 11:44:11 +1100, Lie Ryan wrote:

> 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.


--
Steven

Chris Rebert

unread,
Dec 19, 2009, 11:28:07 PM12/19/09
to Steven D'Aprano, pytho...@python.org
On Sat, Dec 19, 2009 at 8:16 PM, Steven D'Aprano
<st...@remove-this-cybersource.com.au> wrote:
> On Sun, 20 Dec 2009 11:44:11 +1100, Lie Ryan wrote:
>
>> 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?

As they say, the exception proves the rule. :)

Cheers,
Chris
--
http://blog.rebertia.com

Steven D'Aprano

unread,
Dec 20, 2009, 5:15:41 AM12/20/09
to
On Sat, 19 Dec 2009 20:28:07 -0800, Chris Rebert wrote:

>> 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?
>
> As they say, the exception proves the rule. :)

http://www.worldwidewords.org/qa/qa-exc1.htm

--
Steven

Gabriel Genellina

unread,
Dec 21, 2009, 2:33:53 AM12/21/09
to pytho...@python.org
En Sun, 20 Dec 2009 01:16:16 -0300, Steven D'Aprano
<st...@remove-this-cybersource.com.au> escribió:

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."

http://docs.python.org/reference/compound_stmts.html#class-definitions

--
Gabriel Genellina

Daniel Fetchinson

unread,
Dec 21, 2009, 9:03:03 AM12/21/09
to Python
> 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

Steven D'Aprano

unread,
Dec 21, 2009, 5:14:38 PM12/21/09
to
On Mon, 21 Dec 2009 15:03:03 +0100, Daniel Fetchinson wrote:

> 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 :)

Heh heh :)

I actually do care, because (not having a Java/C++ background) I actually
do get a mental "double-take" every time I read about "class variables".
It takes a real effort of will to remind myself that they're probably not
talking about something like this:

for theclass in (int, float, Decimal, Fraction):
do_something_with(theclass)


> I found that his posts are generally useful and helpful, one just has to

> cut all the nitpicking, ...

Technically you don't have to cut *all* the nitpicking, cutting 87.3% of
it is sufficient.


--
Steven

Daniel Fetchinson

unread,
Dec 22, 2009, 3:41:24 AM12/22/09
to Python
>> 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 :)
>
> Heh heh :)
>
> I actually do care, because (not having a Java/C++ background) I actually
> do get a mental "double-take" every time I read about "class variables".
> It takes a real effort of will to remind myself that they're probably not
> talking about something like this:
>
> for theclass in (int, float, Decimal, Fraction):
> do_something_with(theclass)
>

Right. I figured you don't have much of a background in other OO
languages but actually some background helps with python too. For one
it helps communicating with other people on this list who do come from
Java/C++/etc and also because python is heavily influenced by
Java/C++/etc.

Also, programming is not about a specific language but about
programming :) The concepts are general, maybe it's spelled
differently in python and java and C++ etc etc but the programmers in
all these languages do talk about the same stuff.

>> I found that his posts are generally useful and helpful, one just has to
>> cut all the nitpicking, ...
>
> Technically you don't have to cut *all* the nitpicking, cutting 87.3% of
> it is sufficient.

Touche! :)

Cheers,

0 new messages