[Tutor] beginner: using optional agument in __init__ breaks my code
Pawel Kraszewski
Pawel at kraszewscy.net
Mon Jun 26 00:15:07 CEST 2006
Dnia niedziela, 25 czerwca 2006 22:45, Bob Gailer napisaĆ:
> To get the behavior I think you want try:
>
> def __init__(self, q = None):
> if not q: q = []
> self.queue = q
I would disagree... This sure works for empty arguments, but:
>>> u = [1,2,3]
>>> a = Queue(u)
>>> b = Queue(u)
>>> a.insert(12)
>>> print b
gives [1, 2, 3, 12] which is still wrong. However assigning self.queue a COPY
of parameter (empty or not) gives the desired result:
def __init__(self, q = []):
self.queue = q[:]
--
Pawel Kraszewski
More information about the Tutor
mailing list