problem manipulating a list belonging to a class

Steve Howell showell30 at yahoo.com
Sun Nov 22 18:14:00 EST 2009


On Nov 22, 2:50 pm, Marc Leconte <marcg... at 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...



More information about the Python-list mailing list