By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,587 Members | 1,667 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,587 IT Pros & Developers. It's quick & easy.

Python instances

P: n/a
Hi,

How do python instances work?
Why does the code at the end of my posting produce this output:

list in a:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list in b:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

instead of

list in a:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list in b:
[]

----------------------------

class MyClass:
list = []

def add(self, x):
self.list.append(x)

def printer(self):
print self.list

a = MyClass()
b = MyClass()

for n in range(10):
a.add(n)

print "list in a:"
a.printer()
print "list in b:"
b.printer()

/H

Jul 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hi,
class MyClass:
list = []


you have "list" defined as a classmember, not an instancemember. So
"list" ist defined ONCE for all instances.

Try this instead:

class MyClass:
def __init__(self):
self.list = []
[...]

and use self.list ...

HtH, Roland
Jul 19 '05 #2

P: n/a
Guess i shouldn't think of the __init__(self) function as a constructor
then.
Thanks.

/H

Jul 19 '05 #3

P: n/a
Guess i shouldn't think of the __init__(self) function as a constructor
then.

__init__ is THE constructor in Python
--
__________________________________________________ _______________
Laszlo Nagy web: http://designasign.biz
IT Consultant mail: ga*****@geochemsource.com

Python forever!
Jul 19 '05 #4

P: n/a
On 20 Apr 2005 00:44:53 -0700, he***********@hotmail.com wrote:
Guess i shouldn't think of the __init__(self) function as a constructor
then.
Thanks.

Depends on what you think when you think "constructor" ;-)
Read about both __new__ and __init__. The former is always
necessary to create an object, and __init__ may take parameters to
define intial state from its parameters, but __new__ does the whole
job for immutables. I.e., "constructor" translates to combination of
both if both are present, but __new__ must be always be there and
come first. In general there are default methods inherited from
object and/or type, the most primitive classes, so you don't have
to define them except to customize for your purposes.
At least, that's the way I think of it ;-)

Regards,
Bengt Richter
Jul 19 '05 #5

P: n/a
he***********@hotmail.com wrote:
Guess i shouldn't think of the __init__(self) function as a constructor
then.


No, that's not it. You shouldn't think of variables defined outside of a method as instance variables.

In Java for example you can write something like

public class MyClass {
private List list = new ArrayList();

public void add(Object x) {
list.add(x);
}
}

In this case list is a member variable of MyClass instances; 'this' is implicit in Java.

In Python, if you write something that looks similar, the meaning is different:

class MyClass:
list = []

def add(self, x):
self.list.append(x)

In this case, list is an attribute of the class. The Java equivalent is a static attribute. In
Python, instance attributes have to be explicitly specified using 'self'. So instance attributes
have to be bound in an instance method (where 'self' is available):

class MyClass:
def __init__(self):
self.list = []

def add(self, x):
self.list.append(x)

Kent
Jul 19 '05 #6

P: n/a
he***********@hotmail.com wrote:
Hi,

How do python instances work?
Why does the code at the end of my posting produce this output:

list in a:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list in b:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

instead of

list in a:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list in b:
[]

----------------------------

class MyClass:
list = []

def add(self, x):
self.list.append(x)

def printer(self):
print self.list

a = MyClass()
b = MyClass()

for n in range(10):
a.add(n)

print "list in a:"
a.printer()
print "list in b:"
b.printer()

/H


because list is a class member not an instance member (not sure about
the vocabulary) if in __init__ you set self.list=[] you'll get the
result you want! By declaring list=[] in the class it is shared between
all instances!

--
EuGeNe

[----
www.boardkulture.com
www.actiphot.com
www.xsbar.com
----]
Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.