Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > problem manipulating a list belonging to a class

Reply
Thread Tools

problem manipulating a list belonging to a class

 
 
Marc Leconte
Guest
Posts: n/a
 
      11-22-2009
Dear all,

I have a problem with the following code (ubuntu 8.04, Python 2.5.2):

class Toto(object):
def __init__(self, number, mylist=[]):
self.number=number
self.mylist=mylist
pass
pass

listA=Toto(number=1)
listB=Toto(number=2)

listA.mylist.append(5)
print "1) ", listA.mylist
print "2) ", listB.mylist

>> 1) [5]
>> 2) [5]


I would have expected
>> 1) [5]
>> 2) []


Thanks in advance for advice,
Marc.

 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      11-22-2009
Marc Leconte schrieb:

> Dear all,
>
> I have a problem with the following code (ubuntu 8.04, Python 2.5.2):
>
> class Toto(object):
> def __init__(self, number, mylist=[]):
> self.number=number
> self.mylist=mylist
> pass
> pass
>
> listA=Toto(number=1)
> listB=Toto(number=2)
>
> listA.mylist.append(5)
> print "1) ", listA.mylist
> print "2) ", listB.mylist
>
>>> 1) [5]
>>> 2) [5]

>
> I would have expected
>>> 1) [5]
>>> 2) []


http://effbot.org/zone/default-values.htm

Diez
 
Reply With Quote
 
 
 
 
Steve Howell
Guest
Posts: n/a
 
      11-22-2009
On Nov 22, 2:50*pm, Marc Leconte <(E-Mail Removed)> wrote:
> Dear all,
>
> I have a problem with the following code (ubuntu 8.04, Python 2.5.2):
>
> class Toto(object):
> * * * * def __init__(self, number, mylist=[])
> * * * * * * * * self.number=number
> * * * * * * * * self.mylist=mylist
> * * * * * * * * pass
> * * * * pass
>


Change your code to do this:

def __init__(self, number, mylist=None):
if mylist is None:
self.mylist = []
else:
self.mylist = mylist

Explanations of why you need to write it that will follow...
 
Reply With Quote
 
Steve Howell
Guest
Posts: n/a
 
      11-22-2009
On Nov 22, 3:14*pm, Steve Howell <(E-Mail Removed)> wrote:

> Explanations of why you need to write it that will follow...


I knew this had to be written up somewhere...

http://www.ferg.org/projects/python_...ontents_item_6
 
Reply With Quote
 
Lie Ryan
Guest
Posts: n/a
 
      11-23-2009
Marc Leconte wrote:
> class Toto(object):
> def __init__(self, number, mylist=[]):
> self.number=number
> self.mylist=mylist
> pass
> pass


Why are you using pass to end your blocks?
 
Reply With Quote
 
Marc Leconte
Guest
Posts: n/a
 
      11-23-2009
Thx all, good to know

Le dimanche 22 novembre 2009 à 15:16 -0800, Steve Howell a écrit :
> On Nov 22, 3:14 pm, Steve Howell <(E-Mail Removed)> wrote:
>
> > Explanations of why you need to write it that will follow...

>
> I knew this had to be written up somewhere...
>
> http://www.ferg.org/projects/python_...ontents_item_6


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Serving files belonging to the web application directory getsanjay.sharma@gmail.com Java 2 09-30-2007 05:28 PM
Querying for rows belonging to a month =?Utf-8?B?bWF2cmlja18xMDE=?= ASP .Net 3 02-27-2007 01:13 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
A program that prompts the user for keywords belonging to C++. Jason Heyes C++ 3 03-22-2005 02:10 PM
Sorting a dictionary field belonging to a list Jocknerd Python 2 09-17-2004 08:51 PM



Advertisments