Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Unclear On Class Variables

Reply
Thread Tools

Unclear On Class Variables

 
 
Fredrik Lundh
Guest
Posts: n/a
 
      01-13-2005
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.


"set" as in:

obj = foo()
obj.x = 10 # set x

?

if so, the "obj.x=" line is *adding* an instance variable to the "x" object, which will
then hide the "x" at the class level.

>>> class foo(object):

.... x = 0
.... y = 1
....
>>> obj = foo()
>>> obj.__dict__

{}
>>> obj.x

0
>>> obj.y

1
>>> foo.x

0
>>> obj.x = 10
>>> obj.__dict__

{'x': 10}
>>> obj.x

10
>>> foo.x

0

if you want to assign to the class variable, assign to the class variable:

>>> obj = foo()
>>> obj.x

0
>>> foo.x = 20
>>> obj.__dict__

{}
>>> obj.x

20
>>> foo().x

20

</F>



 
Reply With Quote
 
 
 
 
Pierre Barbier de Reuille
Guest
Posts: n/a
 
      01-14-2005
Antoon Pardon a écrit :
>>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
> --------------------
>


Well ... and could someone explain this behaviour ?
I don't catch it !

Pierre
 
Reply With Quote
 
 
 
 
Pierre Barbier de Reuille
Guest
Posts: n/a
 
      01-14-2005
Pierre Barbier de Reuille a écrit :
> Antoon Pardon a écrit :
>
>>> 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
>> --------------------
>>

>
> Well ... and could someone explain this behaviour ?
> I don't catch it !
>
> Pierre


Ok, I think I got it ! I speak with friends working with Python too ...
It seems that "a += l" if "a" and "l" are lists is equivalent to :

a.extend(l)
a = a

The second line could seem meaningless but it is not ! Indeed, in the
example above, the first "sp1.eggs" (the one with the extend) is a class
variable but, the second "sp1.eggs" (the one before the "=") is an
instance variable !

So, at the end, we append to get sp1.eggs and Spam.eggs references to
the same structure. But sp1.eggs is an instance variable of sp1 and no
more the class variable. To test that, it's possible to modify slightly
the code with :

|sp1.eggs += [4,]
|del sp1.eggs

Then, sp1.eggs still exists !!! But it's again the class variable ...

Ok, back to the documentation ...

In the doc, there is a special case for the use of "+=" with the class
members. IMHO, this should not be !!! But, it says that :

ob.a += b

is translated into :

ob.__setattr__( "a", ob.__getattr__("a").__iadd__(b) )

My opinion is : it would be much more simpler to explain than :

a += b <=> a.__iadd__(b); a = a

and not give any special case for class members. In both cases, the
resulting behaviour is the same, but it would be less confusing.

Then, this change of scope of variables in python is very very annoying.
Both for new and old programmers (we have both in my lab ...).

Well, I hope I got it write this time ... but this is a feature to fear !!!

Pierre
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unclear on either EJB3 or JBoss or both Owen Jacobson Java 0 07-21-2006 07:32 AM
Still unclear Irfaan Wahid MCSD 3 02-24-2005 12:14 PM
Scope of news group? Charter unclear Shmuel (Seymour J.) Metz XML 1 07-09-2004 03:41 PM
Unclear on the concept... kj XML 2 04-12-2004 02:49 AM
Re: media player 9/windows xp, video&sound unclear Juan Pérez Computer Support 0 08-08-2003 01:21 AM



Advertisments