471,353 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,353 software developers and data experts.

Lists in classes

Hello,

Learning python from a c++ background. Very confused about this:

============
class jeremy:
list=[]
def additem(self):
self.list.append("hi")
return

temp = jeremy()
temp.additem()
temp.additem()
print temp.list

temp2 = jeremy()
print temp2.list
==============
The output gives:
['hi','hi']
['hi','hi']

Why does adding items to one instance produce items in a separate
instance? Doesn't each instance of jeremy have its' own "list"?

Many thanks for clearing up this newbie confusion.

Jeremy.

Jul 12 '07 #1
9 1140
On 12 jul, 17:23, Jeremy Lynch <jeremy.ly...@gmail.comwrote:
Hello,

Learning python from a c++ background. Very confused about this:

============
class jeremy:
list=[]
def additem(self):
self.list.append("hi")
return

temp = jeremy()
temp.additem()
temp.additem()
print temp.list

temp2 = jeremy()
print temp2.list
==============
The output gives:
['hi','hi']
['hi','hi']

Why does adding items to one instance produce items in a separate
instance? Doesn't each instance of jeremy have its' own "list"?
You've defined list (very bad choice for a name), as a class variable.
To declare instance variable you should have written:
class jeremy:

Jul 12 '07 #2
On Jul 12, 10:23 am, Jeremy Lynch <jeremy.ly...@gmail.comwrote:
Hello,

Learning python from a c++ background. Very confused about this:

============
class jeremy:
list=[]
def additem(self):
self.list.append("hi")
return

temp = jeremy()
temp.additem()
temp.additem()
print temp.list

temp2 = jeremy()
print temp2.list
==============
The output gives:
['hi','hi']
['hi','hi']

Why does adding items to one instance produce items in a separate
instance? Doesn't each instance of jeremy have its' own "list"?

Many thanks for clearing up this newbie confusion.

Jeremy.
The reason it works like that is that your variable "list" isn't an
instance variable per se. Instead, you should have it like this:

<code>

class jeremy:
def __init__(self):
self.lst=[]
def additem(self):
self.lst.append("hi")
return

</code>

Now it works as expected. It's some kind of scope issue, but I can't
explain it adequately.

Mike

Jul 12 '07 #3
On 12 jul, 17:23, Jeremy Lynch <jeremy.ly...@gmail.comwrote:
Hello,

Learning python from a c++ background. Very confused about this:

============
class jeremy:
list=[]
You've defined list (very bad choice of a name, BTW), as a class
variable. To declare is as instance variable you have to prepend it
with "self."

Jul 12 '07 #4
On Jul 12, 6:23 pm, Jeremy Lynch <jeremy.ly...@gmail.comwrote:
Hello,

Learning python from a c++ background. Very confused about this:

============
class jeremy:
list=[]
def additem(self):
self.list.append("hi")
return

temp = jeremy()
temp.additem()
temp.additem()
print temp.list

temp2 = jeremy()
print temp2.list
==============
The output gives:
['hi','hi']
['hi','hi']

Why does adding items to one instance produce items in a separate
instance? Doesn't each instance of jeremy have its' own "list"?

Many thanks for clearing up this newbie confusion.

Jeremy.
You are defining the list in the class context and so it becomes a
class field/member.
For defining instance members you need to always prefix those with
self (this) in the
contexts it is available (f.e. in the instance method context).

bests,

../alex
--
..w( the_mindstorm )p.
Jul 12 '07 #5
Jeremy Lynch wrote:
Hello,

Learning python from a c++ background. Very confused about this:

============
class jeremy:
list=[]
def additem(self):
self.list.append("hi")
return

temp = jeremy()
temp.additem()
temp.additem()
print temp.list

temp2 = jeremy()
print temp2.list
==============
The output gives:
['hi','hi']
['hi','hi']

Why does adding items to one instance produce items in a separate
instance? Doesn't each instance of jeremy have its' own "list"?

Many thanks for clearing up this newbie confusion.

Jeremy.
In this code, "list" (bad name) is a class attribute and all therefor in
all instances, the "list" attribute is reference to the class attribute
unless otherwise assigned, as in __init__.

For instance, try:
temp = jeremy()
temp.additem()
temp.additem()
print temp.list

temp2 = jeremy()
temp2.list = [1,2,3]
print temp.list, temp2.list, jeremy.list
And see which ones look the same (same reference) or look different.

James
Jul 12 '07 #6
Bart Ogryczak wrote:
On 12 jul, 17:23, Jeremy Lynch <jeremy.ly...@gmail.comwrote:
>Hello,

Learning python from a c++ background. Very confused about this:

============
class jeremy:
list=[]

You've defined list (very bad choice of a name, BTW), as a class
variable. To declare is as instance variable you have to prepend it
with "self."

Ouch!

'self' is *not* a reserved ord in python, it doesn't do anything. So
just popping 'self' in front of something doesn't bind it to an instance.
Here is how it works:

class Jeremy(object): # you better inherit from 'object' at all times
classlist = [] # class variable
def __init__(self): # "constructor"
self.instancelist = [] # instance variable
def add_item(self, item):
self.instancelist.append(item)

'self' is the customary name for the first argument of any instance
method, which is always implicitly passed when you call it. I think it
translates to C++'s 'this' keyword, but I may be wrong. Simply put: The
first argument in an instance-method definition (be it called 'self' or
otherwise) refers to the current instance.
Note however that you don't explicitly pass the instance to the method,
that is done automatically:

j = Jeremy() # Jeremy.__init__ is called at this moment, btw
j.add_item("hi") # See? 'item' is the first arg you actually pass

I found this a bit confusing at first, too, but it's actually very
clean, I think.
/W
Jul 12 '07 #7
Thanks for all the replies, very impressive. Got it now.

Jeremy.

On Jul 12, 4:23 pm, Jeremy Lynch <jeremy.ly...@gmail.comwrote:
Hello,

Learning python from a c++ background. Very confused about this:

============
class jeremy:
list=[]
def additem(self):
self.list.append("hi")
return

temp = jeremy()
temp.additem()
temp.additem()
print temp.list
>
temp2 = jeremy()
print temp2.list
==============
The output gives:
['hi','hi']
['hi','hi']

Why does adding items to one instance produce items in a separate
instance? Doesn't each instance of jeremy have its' own "list"?

Many thanks for clearing up this newbie confusion.

Jeremy.

Jul 12 '07 #8
Alex Popescu a écrit :
(snip)
>
You are defining the list in the class context and so it becomes a
class field/member.
'attribute' is the pythonic term.
Jul 12 '07 #9
On Jul 13, 6:02 am, Bruno Desthuilliers
<bdesth.quelquech...@free.quelquepart.frwrote:
Alex Popescu a écrit :
(snip)
You are defining the list in the class context and so it becomes a
class field/member.

'attribute' is the pythonic term.

Thanks! I'm just a couple of weeks Python old, so I am still fighting
to use the correct vocabulary :-).
And as with foreign languages, while I am becoming better at reading I
still have problems expressing
correct ideas using correct words.

../alex
--
..w( the_mindstorm )p.

Jul 15 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

42 posts views Thread by Jeff Wagner | last post: by
7 posts views Thread by Chris Ritchey | last post: by
reply views Thread by Jimmy Cerra | last post: by
1 post views Thread by andrew | last post: by
9 posts views Thread by Paulo da Silva | last post: by
5 posts views Thread by Gordon Airporte | last post: by
1 post views Thread by colin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.