Re: [Tutor] beginner: using optional agument in __init__ breaks my code

2006-06-26 Thread Danny Yoo
>> The values of optional arguments are only once evaluated (when Python >> reads the definition). If you place there mutable objects like e.g. a >> list most of the time the effect you see is not what you want. So you >> have to write it a bit different. >> >> def __init__(self, q = None):

Re: [Tutor] beginner: using optional agument in __init__ breaks my code

2006-06-26 Thread Pawel Kraszewski
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 =

Re: [Tutor] beginner: using optional agument in __init__ breaks my code

2006-06-25 Thread Barbara Schneider
--- Karl Pflästerer <[EMAIL PROTECTED]> schrieb: > > The values of optional arguments are only once > evaluated (when Python > reads the definition). If you place there mutable > objects like e.g. a > list most of the time the effect you see is not what > you want. So you > have to write it a bi

Re: [Tutor] beginner: using optional agument in __init__ breaks my code

2006-06-25 Thread Bob Gailer
Barbara Schneider wrote: > Hello Group, I am puzzled about this: The following > code implements a simple FIFO object. > > class Queue: > "Implementing a FIFO data structure." > > # Methods > def __init__(self): > self.queue = [] > > def emptyP(self): > return (s

Re: [Tutor] beginner: using optional agument in __init__ breaks my code

2006-06-25 Thread Karl Pflästerer
On 25 Jun 2006, [EMAIL PROTECTED] wrote: [...] > This code works as intended. Now my idea is to provide > an optional argument to the constructor. So I change > it to: > > def __init__(self, q =[]): > self.queue = q > > Now, something very strange happens: > a = Queue() b =

[Tutor] beginner: using optional agument in __init__ breaks my code

2006-06-25 Thread Barbara Schneider
Hello Group, I am puzzled about this: The following code implements a simple FIFO object. class Queue: "Implementing a FIFO data structure." # Methods def __init__(self): self.queue = [] def emptyP(self): return (self.queue == []) def insert(self, item):