Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

problem manipulating a list belonging to a class

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


marcglec at free

Nov 22, 2009, 2:50 PM

Post #1 of 7 (333 views)
Permalink
problem manipulating a list belonging to a class

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.

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


deets at nospam

Nov 22, 2009, 3:10 PM

Post #2 of 7 (312 views)
Permalink
Re: problem manipulating a list belonging to a class [In reply to]

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
--
http://mail.python.org/mailman/listinfo/python-list


showell30 at yahoo

Nov 22, 2009, 3:14 PM

Post #3 of 7 (312 views)
Permalink
Re: problem manipulating a list belonging to a class [In reply to]

On Nov 22, 2:50 pm, Marc Leconte <marcg...@free.fr> 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...
--
http://mail.python.org/mailman/listinfo/python-list


showell30 at yahoo

Nov 22, 2009, 3:16 PM

Post #4 of 7 (316 views)
Permalink
Re: problem manipulating a list belonging to a class [In reply to]

On Nov 22, 3:14 pm, Steve Howell <showel...@yahoo.com> 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_gotchas.html#contents_item_6
--
http://mail.python.org/mailman/listinfo/python-list


lie.1296 at gmail

Nov 22, 2009, 7:55 PM

Post #5 of 7 (302 views)
Permalink
Re: problem manipulating a list belonging to a class [In reply to]

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?
--
http://mail.python.org/mailman/listinfo/python-list


tjreedy at udel

Nov 23, 2009, 1:43 AM

Post #6 of 7 (299 views)
Permalink
Re: problem manipulating a list belonging to a class [In reply to]

Marc Leconte 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
>
> 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,

Read the Python FAQ and or the reference manual section on def
statements (function definition) which specifically has a paragraph with
a bold-face statement explaining this.



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


marcglec at free

Nov 23, 2009, 10:47 AM

Post #7 of 7 (294 views)
Permalink
Re: problem manipulating a list belonging to a class [In reply to]

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 <showel...@yahoo.com> 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_gotchas.html#contents_item_6

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

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.