473,382 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

python bug?

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
Jul 18 '05 #1
6 1203
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

Jul 18 '05 #2
Anthony Petropoulos wrote:
When running this simple code:
-------
class Dada:
a = []
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
Jul 18 '05 #3
On Mon, 26 Jul 2004, Anthony Petropoulos wrote:
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'].


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

Jul 18 '05 #4
"Anthony Petropoulos" <ap*****@gmail.com> wrote in message
news:ma*************************************@pytho n.org...
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]

--
I don't actually read my hotmail account, but you can replace hotmail with
excite if you really want to reach me.
Jul 18 '05 #5
Is this what you meant to do?
class Dada: .... 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 Petropoulos wrote:
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

Jul 18 '05 #6
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.
On Mon, 26 Jul 2004 17:27:31 +0200, Thomas Guettler
<gu*****@thomas-guettler.de> wrote:
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

--
http://mail.python.org/mailman/listinfo/python-list

Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.