Go Back   Velocity Reviews > Newsgroups > Python
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Python - Unclear On Class Variables

 
Thread Tools Search this Thread
Old 01-13-2005, 12:18 PM   #1
Default Unclear On Class Variables


I am a bit confused. I was under the impression that:

class foo(object):
x = 0
y = 1

means that x and y are variables shared by all instances of a class.
But when I run this against two instances of foo, and set the values
of x and y, they are indeed unique to the *instance* rather than the
class.

It is late and I am probably missing the obvious. Enlightenment appreciated ...
--
----------------------------------------------------------------------------
Tim Daneliuk
PGP Key: http://www.tundraware.com/PGP/


Tim Daneliuk
  Reply With Quote
Old 01-13-2005, 12:36 PM   #2
Diez B. Roggisch
 
Posts: n/a
Default Re: Unclear On Class Variables
Tim Daneliuk wrote:

> I am a bit confused. I was under the impression that:
>
> class foo(object):
> x = 0
> y = 1
>
> means that x and y are variables shared by all instances of a class.
> But when I run this against two instances of foo, and set the values
> of x and y, they are indeed unique to the *instance* rather than the
> class.
>
> It is late and I am probably missing the obvious. Enlightenment
> appreciated ...


Without actual code how you set the vars, no one can answer that question.

--
Regards,

Diez B. Roggisch


Diez B. Roggisch
  Reply With Quote
Old 01-13-2005, 12:41 PM   #3
Simon Brunning
 
Posts: n/a
Default Re: Unclear On Class Variables
On 13 Jan 2005 07:18:26 EST, Tim Daneliuk <> wrote:
> I am a bit confused. I was under the impression that:
>
> class foo(object):
> x = 0
> y = 1
>
> means that x and y are variables shared by all instances of a class.
> But when I run this against two instances of foo, and set the values
> of x and y, they are indeed unique to the *instance* rather than the
> class.


I can see why you might think that:

>>> class Spam(object):

.... eggs = 4
....
>>> spam = Spam()
>>> spam2 = Spam()
>>> spam.eggs

4
>>> spam2.eggs

4
>>> spam.eggs = 2
>>> spam.eggs

2
>>> spam2.eggs

4

But you are being mislead by the fact that integers are immutable.
'spam.eggs = 2' is *creating* an instance member - there wasn't one
before. Have a look at what happens with a mutable object:

>>> class Spam(object):

.... eggs = [3]
....
>>> spam = Spam()
>>> spam2 = Spam()
>>> spam.eggs

[3]
>>> spam2.eggs

[3]
>>> spam.eggs.append(5)
>>> spam.eggs

[3, 5]
>>> spam2.eggs

[3, 5]

--
Cheers,
Simon B,
,
http://www.brunningonline.net/simon/blog/


Simon Brunning
  Reply With Quote
Old 01-13-2005, 12:45 PM   #4
harold fellermann
 
Posts: n/a
Default Re: Unclear On Class Variables
Hi Tim,

If you have

class Foo(object) :
x = 0
y = 1

foo = Foo()

foo.x # reads either instance or class attribute (class in this case)

foo.x = val # sets an instance attribute (because foo is instance not
class)

Foo.x = val # sets a class attribute
foo.__class.__x = val # does the same

this might be sometimes confusing. IMHO, the following is especially
nasty:

>>> foo = Foo()
>>> foo.x += 1
>>>
>>> print foo.x

1
>>> print Foo.x

0

although the += operator looks like an inplace add it isn't.
it is just syntactic sugar for foo.x = foo.x + 1.


- harold -


On 13.01.2005, at 07:18, Tim Daneliuk wrote:

> I am a bit confused. I was under the impression that:
>
> class foo(object):
> x = 0
> y = 1
>
> means that x and y are variables shared by all instances of a class.
> But when I run this against two instances of foo, and set the values
> of x and y, they are indeed unique to the *instance* rather than the
> class.
>
> It is late and I am probably missing the obvious. Enlightenment
> appreciated ...
> --
> -----------------------------------------------------------------------
> -----
> Tim Daneliuk
> PGP Key: http://www.tundraware.com/PGP/
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>

--
Everyone is a genius.
It's just that some people are too stupid to realize it.



harold fellermann
  Reply With Quote
Old 01-13-2005, 01:26 PM   #5
Antoon Pardon
 
Posts: n/a
Default Re: Unclear On Class Variables
Op 2005-01-13, Simon Brunning schreef <>:
> On 13 Jan 2005 07:18:26 EST, Tim Daneliuk <> wrote:
>> I am a bit confused. I was under the impression that:
>>
>> class foo(object):
>> x = 0
>> y = 1
>>
>> means that x and y are variables shared by all instances of a class.
>> But when I run this against two instances of foo, and set the values
>> of x and y, they are indeed unique to the *instance* rather than the
>> class.

>
> I can see why you might think that:
>
>>>> class Spam(object):

> ... eggs = 4
> ...
>>>> spam = Spam()
>>>> spam2 = Spam()
>>>> spam.eggs

> 4
>>>> spam2.eggs

> 4
>>>> spam.eggs = 2
>>>> spam.eggs

> 2
>>>> spam2.eggs

> 4
>
> But you are being mislead by the fact that integers are immutable.
> 'spam.eggs = 2' is *creating* an instance member - there wasn't one
> before. Have a look at what happens with a mutable object:
>
>>>> class Spam(object):

> ... eggs = [3]
> ...
>>>> spam = Spam()
>>>> spam2 = Spam()
>>>> spam.eggs

> [3]
>>>> spam2.eggs

> [3]
>>>> spam.eggs.append(5)
>>>> spam.eggs

> [3, 5]
>>>> spam2.eggs

> [3, 5]
>


Well I find this a confusing behaviour on python's part. The fact
that instance.field can mean something different, depending on
where in a statement you find it, makes the behaviour inconsistent.

I know people in general here are against declarations, but declarations
could IMO provide more consistency here and thus more obvious behaviour.

--
Antoon Pardon


Antoon Pardon
  Reply With Quote
Old 01-13-2005, 01:33 PM   #6
Antoon Pardon
 
Posts: n/a
Default Re: Unclear On Class Variables
Op 2005-01-13, harold fellermann schreef <>:
> Hi Tim,
>
> If you have
>
> class Foo(object) :
> x = 0
> y = 1
>
> foo = Foo()
>
> foo.x # reads either instance or class attribute (class in this case)
>
> foo.x = val # sets an instance attribute (because foo is instance not
> class)
>
> Foo.x = val # sets a class attribute
> foo.__class.__x = val # does the same
>
> this might be sometimes confusing. IMHO, the following is especially
> nasty:
>
> >>> foo = Foo()
> >>> foo.x += 1
> >>>
> >>> print foo.x

> 1
> >>> print Foo.x

> 0
>
> although the += operator looks like an inplace add it isn't.
> it is just syntactic sugar for foo.x = foo.x + 1.


Except is x belongs to a mutable class that implements the
+= operator as an inplace add.

Try the same but with x = [2]
and foo.x += [3]

--
Antoon Pardon


Antoon Pardon
  Reply With Quote
Old 01-13-2005, 01:56 PM   #7
Peter Hansen
 
Posts: n/a
Default Re: Unclear On Class Variables
Simon Brunning wrote:
> On 13 Jan 2005 07:18:26 EST, Tim Daneliuk <> wrote:
> But you are being mislead by the fact that integers are immutable.
> 'spam.eggs = 2' is *creating* an instance member - there wasn't one
> before. Have a look at what happens with a mutable object:


Simon, it's really not about mutability at all. You've changed
the example, which was binding a name (specifically setting an
attribute), to one in which you are simply calling a method on
the object. If you change your example to bind the name the
same way, even with a mutable, it will work the same way as Tim's
original did with integers:

>>> class Spam(object):

.... eggs = [3]
....
>>> spam = Spam()
>>> spam2 = Spam()
>>> spam.eggs = [7]
>>> spam2.eggs

[3]

-Peter


Peter Hansen
  Reply With Quote
Old 01-13-2005, 02:16 PM   #8
Simon Brunning
 
Posts: n/a
Default Re: Unclear On Class Variables
On Thu, 13 Jan 2005 08:56:10 -0500, Peter Hansen <> wrote:
> Simon, it's really not about mutability at all. You've changed
> the example,


Err, there *wasn't* an example, not really. The OP just mentioned
'setting the values' of instance members. That *can* mean name
binding, but (to my mind at least) it can also mean calling mutating
methods. I just wanted to show both.

--
Cheers,
Simon B,
,
http://www.brunningonline.net/simon/blog/


Simon Brunning
  Reply With Quote
Old 01-13-2005, 02:46 PM   #9
Antoon Pardon
 
Posts: n/a
Default Re: Unclear On Class Variables
>
> Well I find this a confusing behaviour on python's part. The fact
> that instance.field can mean something different, depending on
> where in a statement you find it, makes the behaviour inconsistent.
>
> I know people in general here are against declarations, but declarations
> could IMO provide more consistency here and thus more obvious behaviour.


Well just to show how confusing python can be, the following piece of
code.

| class Spam:
| eggs = [2, 3]
|
|
| sp1 = Spam()
| sp2 = Spam()
|
| print sp1.eggs, id(sp1.eggs)
| print sp2.eggs, id(sp2.eggs)
| print '--------------------'
|
| sp1.eggs += [4,]
|
| print sp1.eggs, id(sp1.eggs)
| print sp2.eggs, id(sp2.eggs)
| print '--------------------'
|
| Spam.eggs = [3,5]
|
| print sp1.eggs, id(sp1.eggs)
| print sp2.eggs, id(sp2.eggs)
| print '--------------------'

Which produces:

[2, 3] 1075958860
[2, 3] 1075958860
--------------------
[2, 3, 4] 1075958860
[2, 3, 4] 1075958860
--------------------
[2, 3, 4] 1075958860
[3, 5] 1075959084
--------------------



Antoon Pardon
  Reply With Quote
Old 01-13-2005, 02:50 PM   #10
Steve Holden
 
Posts: n/a
Default Re: Unclear On Class Variables
Tim Daneliuk wrote:

> I am a bit confused. I was under the impression that:
>
> class foo(object):
> x = 0
> y = 1
>
> means that x and y are variables shared by all instances of a class.


What it actually does is define names with the given values *in the
class namespace*.

> But when I run this against two instances of foo, and set the values
> of x and y, they are indeed unique to the *instance* rather than the
> class.
>

I imagine here you are setting instance variables, which then *mask* the
presence of class variables with the same name, because "self-relative"
name resolution looks in the instance namespace before it looks in the
class namespace.

> It is late and I am probably missing the obvious. Enlightenment
> appreciated ...


You can refer to class variables using the class name explicitly, both
within methods and externally:

>>> class X:

... count = 0
... def getCt(self):
... return self.count
... def inc(self):
... self.count += 1
...
>>> x1 = X()
>>> x2 = X()
>>> id(x1.count)

168378284
>>> x1.inc()
>>> id(x1.count)

168378272
>>> id(x2.count)

168378284
>>> id(X.count)

168378284
>>> x1.getCt()

1
>>> x2.getCt()

0
>>>


regards
Steve
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Holden Web LLC +1 703 861 4237 +1 800 494 3119



Steve Holden
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
loading java class from .war file jayeth Software 0 04-09-2009 07:06 PM
Eclipse how to add external class with different pkg name jan2321 General Help Related Topics 0 11-06-2008 10:04 AM
Custom Class Loader for Web Application using Tomcat tapas.adhikary Software 0 04-22-2008 09:53 AM
70-536, 3 questions blade MCTS 11 03-23-2008 03:47 PM
Storing class with session (ASP.Net) bqmassey Software 0 09-22-2006 05:37 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46