data validation when creating an object

Roy Smith roy at panix.com
Thu Jan 16 00:05:07 EST 2014


In article <mailman.5567.1389848051.18130.python-list at python.org>,
 Ben Finney <ben+python at benfinney.id.au> wrote:

> Roy Smith <roy at panix.com> writes:
> > But, Python is not C++. I suspect the people who argue for __init__()
> > not doing much are extrapolating a C++ pattern to other languages
> > without fully understanding the reason why.
> 
> Even simpler: They are mistaken in what the constructor is named, in
> Python.
> 
> Python classes have the constructor, ‘__new__’. I would agree with
> advice not to do anything but allocate the resources for a new instance
> in the constructor.

I've always found this distinction to be somewhat silly.

C++ constructors are also really just initializers.  Before your 
constructor is called, something else (operator new, at least for 
objects in the heap) has already allocated memory for the object.  It's 
the constructor's job to initialize the data.  That's really very much 
the same distinction as between __new__() and __init__().



More information about the Python-list mailing list