default value in __init__

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Fri Oct 10 13:11:21 CEST 2008

David C. Ullrich a écrit :
> In article 
> <5f3a6fdc-40e5-4450-b65d-066f87f27309 at>,
>  kenneth <kenneth at> wrote:
>> On Oct 9, 10:14 am, Christian Heimes <li... at> 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.
>>> .
>> 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 ?"

Well... How to say.. Is there any chance these people will read anything 
*at all* ?

More information about the Python-list mailing list