problem manipulating a list belonging to a class

Discussion in 'Python' started by Marc Leconte, Nov 22, 2009.

  1. Marc Leconte

    Marc Leconte Guest

    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.
     
    Marc Leconte, Nov 22, 2009
    #1
    1. Advertising

  2. 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
     
    Diez B. Roggisch, Nov 22, 2009
    #2
    1. Advertising

  3. Marc Leconte

    Steve Howell Guest

    On Nov 22, 2:50 pm, 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
    >


    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...
     
    Steve Howell, Nov 22, 2009
    #3
  4. Marc Leconte

    Steve Howell Guest

    Steve Howell, Nov 22, 2009
    #4
  5. Marc Leconte

    Lie Ryan Guest

    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?
     
    Lie Ryan, Nov 23, 2009
    #5
  6. Marc Leconte

    Marc Leconte Guest

    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 <> 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
     
    Marc Leconte, Nov 23, 2009
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason Heyes
    Replies:
    3
    Views:
    388
    Thomas Matthews
    Mar 22, 2005
  2. Jocknerd
    Replies:
    2
    Views:
    332
    John Lenton
    Sep 17, 2004
  3. =?Utf-8?B?bWF2cmlja18xMDE=?=

    Querying for rows belonging to a month

    =?Utf-8?B?bWF2cmlja18xMDE=?=, Feb 26, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    315
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Feb 27, 2007
  4. manipulating linked list

    , Aug 3, 2006, in forum: C Programming
    Replies:
    6
    Views:
    372
    Chris Torek
    Aug 7, 2006
  5. Replies:
    2
    Views:
    314
Loading...

Share This Page