Class vs instance variables? 
Author Message
 Class vs instance variables?

Sorry if this is a FAQ item but <insert lame excuse here>.
I am trying to figure out what data is shared btw instances
and what not. Could someone please explain this behavior:

class c:
    a=[]
    x=0
    def __init__(self,x):
        self.x=x
        self.a.append(x)

Quote:
>>> c1=c(1)
>>> c1.x
1
>>> c1.a
[1]

>>> c2=c(2)
>>> c2.x
2
>>> c1.x

1
# so far, so good; c1 and c2 have their own instance variables x,
# and c1.x stays the same after creating c2. But:
Quote:
>>> c1.a
[1, 2]
>>> c2.a

[1, 2]
# ??? Why is this list shared while the scalars are separate?
# How do I tell python to make the list an instance var,
# or the scalar a class var?

--
Sandor Markon
FUJITEC Co.Ltd. (G.O.C.), 28-10, Shoh 1-chome, Ibaraki, Osaka, JAPAN



Mon, 29 Oct 2001 03:00:00 GMT  
 Class vs instance variables?

Quote:

> I am trying to figure out what data is shared btw instances
> and what not. Could someone please explain this behavior:

I'll try but I'm relatively new to python too...

Quote:
> class c:
>     a=[]

This is a class variable referencing a list

Quote:
>     x=0

This is a class variable referencing an int

Quote:
>     def __init__(self,x):
>         self.x=x

This creates a new instance variable which 'hides' the
class variable and has value x.
Assignment in python creates a new reference.

Quote:
>         self.a.append(x)

This *modifies* the existing reference(the class variable)
If you had done self.a = [1,2,3] you would have gotten
an instance based list

Quote:
> # ??? Why is this list shared while the scalars are separate?

Its nothing to do with the list/scalar asp[ects its to do
with assignment versus modification.
- assignment creates a new instance variable.

At least thats hiw I underSTAND IT...

Alan g.

--
=================================================
This post represents the views of the author
and does not necessarily accurately represent
the views of BT.



Mon, 29 Oct 2001 03:00:00 GMT  
 Class vs instance variables?

Quote:

> Sorry if this is a FAQ item but <insert lame excuse here>.
> I am trying to figure out what data is shared btw instances
> and what not. Could someone please explain this behavior:

> class c:
>     a=[]
>     x=0
>     def __init__(self,x):
>         self.x=x
>         self.a.append(x)
> # ??? Why is this list shared while the scalars are separate?

BOTH are shared.

it's just that you REPLACE the binding for the integer
(self.x = x creates a new instance binding for x, hiding
the one in the class), while you MODIFY the list in place.

no magic involved.

Quote:
> # How do I tell Python to make the list an instance var,
> # or the scalar a class var?

to make class variables behave like class variables, you
need to access them through the class' namespace (use
c.a or c.x instead of self.a and self.x).

but you may wish to avoid class variables if you possibly
can. "global" (that is, module variables, not program globals)
variables are often easier to use.

</F>

PS. no chance you can teach your newsreader not to
mark your messages as "iso-2022-jp" when they only con-
tain plain ASCII?



Mon, 29 Oct 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Newbie Tip: Classes and variable passing vs instances from ENGSOL

2. instances of classes and instances of instances

3. Class vs Instance methods

4. Creating a instance variable with a new class.

5. Class-instance variables

6. Class instance variables

7. Class vs Instance methods

8. class instance variable question

9. Class instance variable in package

10. Class side vs Instance Side??

11. Class methods vs Instance methods

12. class instance variables in other languages?

 

 
Powered by phpBB® Forum Software