[Python-Dev] Proper initialization of structs

Christian Heimes lists at cheimes.de
Fri Oct 31 00:43:46 CET 2008

Alexandre Vassalotti wrote:
> And that is exactly the reason why, the _pickle module doesn't use
> __new__ for initialization.  Doing any kind of argument parsing in
> __new__ prevents subclasses from customizing the arguments for their
> __init__.
> Although, I agree that __new__ should be used, whenever it is
> possible, to initialize struct members.

You are missunderstanding me. I want everybody to set the struct members 
to *A* sensible default value, not *THE* value. Argument parsing can 
still happen in tp_init. tp_new should (or must?) set all struct members 
to sensible defaults like NULL for pointers, -1 or 0 for numbers etc.

Python uses malloc to allocate memory. Unless you are using debug builds 
the memory block is not initialized. In both cases the block of memory 
isn't zeroed. You all know the problems caused by uninitialized memory.


More information about the Python-Dev mailing list