def __init__(self):

Steven D'Aprano steve at pearwood.info
Tue Apr 26 12:32:53 EDT 2016


On Wed, 27 Apr 2016 02:26 am, Random832 wrote:

> On Tue, Apr 26, 2016, at 12:12, Steven D'Aprano wrote:
>> The obvious reason for overriding __new__ is to construct an immutable
>> instance. You have to override __new__, because by the time it returns
>> the
>> instance is immutable and you can no longer initialise it.
> 
> Other than by subclassing an existing immutable type written in C, is it
> actually possible to define a truly-immutable (rather than
> contract-immutable) class in python?

No.

Subclassing immutable built-ins is the most obvious and simple (and probably
common) way to get an immutable class. Actually immutable, short of doing
wicked things with ctypes.


> (Of course, subclassing immutable C types is the more obvious answer to
> when you have to override __new__).

Indeed.


-- 
Steven




More information about the Python-list mailing list