python bug?

  • Thread starter Anthony Petropoulos
  • Start date
A

Anthony Petropoulos

Hi,

When running this simple code:
-------
class Dada:
a = []
def __init__(self, arg):
self.a.append(arg)

d1 = Dada("pro")
d2 = Dada("hoho")

print d1.a, d2.a
------------
I get the output ['pro', 'hoho'] ['pro', 'hoho'], instead of what I
expected: ['pro'] ['hoho'].

Is this a feature? Is there something I'm missing?

Anthony Petropoulos
 
P

Peter Hansen

Anthony said:
When running this simple code:

This defines a class variable, of which there exists
only one, effectively shared across all instances of
Dada.
def __init__(self, arg):
self.a.append(arg)

This (the "self.a" part) looks up the name "a" in the
dictionary for the instance. It doesn't find anything
there, so it looks next in the class. It finds the
shared class variable. Then you append something to
it. Note that as you are not "rebinding" the name
"self.a" to anything, but merely modifying the contents
of the list it is bound to, you don't end up with an
instance variable called "self.a".
d1 = Dada("pro")
d2 = Dada("hoho")

Given the above, it should be apparent that this is
just appending both values to the same list.
Is this a feature? Is there something I'm missing?

Yes, you are missing the following change to your code
to do what you want:

class Dada:
def __init__(self, arg):
self.a = []
self.a.append(arg)

(of course, you would probably just write that as a
single line "self.a = [arg]" in real code)

This stuff should be covered, I believe, by the tutorial.
Make sure you have gone through that carefully, and perhaps
take the time to read all the FAQ entries before getting too
deep into the language.

-Peter
 
C

Christopher T King

When running this simple code:
-------
class Dada:
a = []
def __init__(self, arg):
self.a.append(arg)

d1 = Dada("pro")
d2 = Dada("hoho")

print d1.a, d2.a

In your code, 'a' is initialized at the class level. This means that it
is only initialized once, when the class is first created. Each time you
instantiate a new object, it's using the same list bound to a. To do what
you want, you need to initialize 'a' for each object, rather than the
class as a whole:

class Dada:
def __init__(self, arg):
self.a = []
self.a.append(arg)

d1 = Dada("pro")
d2 = Dada("hoho")

print d1.a, d2.a # -> ['pro'] ['hoho']
 
R

Russell Blau

Anthony Petropoulos said:
Hi,

When running this simple code:
-------
class Dada:
a = []
def __init__(self, arg):
self.a.append(arg)

d1 = Dada("pro")
d2 = Dada("hoho")

print d1.a, d2.a
------------
I get the output ['pro', 'hoho'] ['pro', 'hoho'], instead of what I
expected: ['pro'] ['hoho'].

Is this a feature? Is there something I'm missing?

You established "a" as an attribute at the class level, not at the instance
level. Then you had each instance append something to the class attribute.
The output is correct; it is your expectation that needs to be modified.
:)

To see the difference, try changing the body of your __init__ method to
read:
self.a = [arg]
 
T

Thomas Guettler

Am Mon, 26 Jul 2004 16:21:51 +0300 schrieb Anthony Petropoulos:
Hi,

When running this simple code:
-------
class Dada:
a = []
def __init__(self, arg):
self.a.append(arg)

d1 = Dada("pro")
d2 = Dada("hoho")

print d1.a, d2.a
------------
I get the output ['pro', 'hoho'] ['pro', 'hoho'], instead of what I
expected: ['pro'] ['hoho'].

Is this a feature? Is there something I'm missing?

your list "a" is at "class level". All instances
share the same.

try this:
def __init__(self, arg):
self.a=[]
self.a.append(arg)

HTH,
Thomas
 
J

Jeff Sandys

Is this what you meant to do?
.... a = []
.... def __init__(self,arg):
.... Dada.a.append(arg)
.... self.a = Dada.a
....
d1 = Dada("pro")
d2 = Dada("hobo")
d1.a ['pro', 'hobo']
d2.a ['pro', 'hobo']
Dada.a ['pro', 'hobo']

Thanks,
Jeff Sandys

Anthony said:
Hi,

When running this simple code:
-------
class Dada:
a = []
def __init__(self, arg):
self.a.append(arg)

d1 = Dada("pro")
d2 = Dada("hoho")

print d1.a, d2.a
------------
I get the output ['pro', 'hoho'] ['pro', 'hoho'], instead of what I
expected: ['pro'] ['hoho'].

Is this a feature? Is there something I'm missing?

Anthony Petropoulos
 
A

Anthony Petropoulos

Thanks to all who answered.

I was just confused because if I used assignment instead of .append
the thing worked. Too much java I guess *ducks*.

Peter Hansen's mail (among others') cleared it up for me.


Am Mon, 26 Jul 2004 16:21:51 +0300 schrieb Anthony Petropoulos:


Hi,

When running this simple code:
-------
class Dada:
a = []
def __init__(self, arg):
self.a.append(arg)

d1 = Dada("pro")
d2 = Dada("hoho")

print d1.a, d2.a
------------
I get the output ['pro', 'hoho'] ['pro', 'hoho'], instead of what I
expected: ['pro'] ['hoho'].

Is this a feature? Is there something I'm missing?

your list "a" is at "class level". All instances
share the same.

try this:
def __init__(self, arg):
self.a=[]
self.a.append(arg)

HTH,
Thomas
 

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

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,665
Latest member
salkete

Latest Threads

Top