Python instances

H

henrikpierrou

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
 
R

Roland Heiber

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
 
H

henrikpierrou

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

/H
 
L

Laszlo Zsolt Nagy

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: (e-mail address removed)

Python forever!
 
B

Bengt Richter

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
 
K

Kent Johnson

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
 
E

EuGeNe

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
----]
 
M

M.E.Farmer

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
As others have noted, you are using a class name and not an instance
name.
Class names are shared by all instances.Instance names are not shared.
Also this next bit can bite you.
Don't use list it is builtin name and can cause problems when you
rebind it.
Others to avoid are keywords and file, dict, tuple, object, etc...
They can be tempting to use, resist, it will save you debug time later.
hth,
M.E.Farmer
 
B

Bengt Richter

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)

The following shows nothing static anywhere, yet a class has been defined, an instance created, and
__init__ called with initial value, and the value retrieved as an attribute of the returned instance,
and it's all an expression.
'hello'

Regards,
Bengt Richter
 
K

Kent Johnson

Bengt said:
The following shows nothing static anywhere, yet a class has been defined, an instance created, and
__init__ called with initial value, and the value retrieved as an attribute of the returned instance,
and it's all an expression.

'hello'

I have no idea what point you are trying to make, except maybe that it is possible to obfuscate a
simple class definition.

Kenw
 
B

Bengt Richter

I have no idea what point you are trying to make, except maybe that it is possible to obfuscate a
simple class definition.
I may have misunderstood your point ;-) I was just trying to add an exclamation point.
I.e., everything happening above shows that you don't need to have a name bound to the class
as in class C:pass, nor do you need to "put" and instance "somewhere", as in in inst = C('hello'),
and you don't need to do that to get inst.foo from all of the preceding. I.e., whatever the OP
thinks about assigning/binding, it's not an absolutely necessary part of any of the preceding.

Regards,
Bengt Richter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top