[Tutor] beginner: using optional agument in __init__ breaks my code
Barbara Schneider
brb.shneider at yahoo.de
Sun Jun 25 23:03:00 CEST 2006
--- Karl Pflästerer <khp at pflaesterer.de> 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 bit different.
>
> def __init__(self, q = None):
> if not q: q = []
> self.queue = q
>
> or
>
> def __init__(self, q = None):
> self.queue = q or []
>
> Now you get a fresh list for each instance.
>
> Karl
Thank you very much. I will use your code as a
"recipe", while I still try to understand the
mechanism and the reasons behind it. For me this feels
odd.
Barb
___________________________________________________________
Gesendet von Yahoo! Mail - Jetzt mit 1GB Speicher kostenlos - Hier anmelden: http://mail.yahoo.de
More information about the Tutor
mailing list