In article <[EMAIL PROTECTED]>, Bruno Desthuilliers <[EMAIL PROTECTED]> wrote:
> David C. Ullrich a écrit : > > In article > > <[EMAIL PROTECTED]>, > > kenneth <[EMAIL PROTECTED]> wrote: > > > >> On Oct 9, 10:14 am, Christian Heimes <[EMAIL PROTECTED]> wrote: > >>> kenneth wrote: > >>>> the 'd' variable already contains the 'self.d' value of the first > >>>> instance and not the default argument {}. > >>>> Am I doing some stupid error, or this is a problem ? > >>> No, it always contains the default argument because default values are > >>> created just ONE > >>> TIME.http://effbot.org/pyfaq/why-are-default-values-shared-between-objects > >>> .. > >>> . > >> > >> Wow, it's a very "dangerous" behavior ... > >> > >> Just to know, is this written somewhere in the python documentation or > >> one has to discover it when his programs fails to work ;-) ? > > > > At least once a week someone discovers this "problem", makes a > > post about it here, and then someone points to the spot in the > > documentation where it's explained. > > > > Seems to me that people often site the "important warning" in > > the tutorial. Of course there's no reason anyone would bother > > going through the tutorial > > Indeed. No reason at all. > > > - just for fun I looked in the > > official Python Reference Manual to see whether they're explicit > > about this or require the reader to figure it out from something > > else they say. > > > > There's a section titled "7.6 Function definitions". About halfway > > through that section there's a _bold face_ statement > > "Default parameter values are evaluated when the function definition is > > executed.", followed by an explanation of how that can lead to > > the sort of problem above. > > But there's no reason to read the reference manual neither. > > > So I guess it _is_ awfully dangerous. They should really explain > > this aspect of the language's behavior to people who don't read > > the formal definition and also don't work through the tutorial. > > You mean : "to people that don't bother reading the FineManual *nor* > searching the newsgroup / ML archives ?" Yes. Also add "don't read any books". I think I started with some book - the book pointed out the "surprise" you get when you say L = [[0]] * 10 L[0][0] = 1 or something similar. I was probably surprised once by the mutable default parameter thing once but having understood that other example it wasn't hard to see what was happening. > Well... How to say.. Is there any chance these people will read anything > *at all* ? No. That's exactly the point! Basic Python is so transparent that you can start using it without reading anything, just looking at a few examples. _Because_ of that it's their responsibility to ensure that if you look at a few examples you then have a complete understanding of the language. In particular default parameters should work the way the user expects! The fact that different users will expect different things here is no excuse... -- David C. Ullrich
-- http://mail.python.org/mailman/listinfo/python-list