Making immutable instances

Steven D'Aprano steve at REMOVETHIScyber.com.au
Fri Nov 25 22:39:30 EST 2005


On Fri, 25 Nov 2005 20:50:41 -0500, Mike Meyer wrote:

> Steven D'Aprano <steve at REMOVETHIScyber.com.au> writes:
>> On Thu, 24 Nov 2005 11:44:16 -0500, Mike Meyer wrote:
>>> In Python, I can even fix
>>> it so *your* code uses my wrapped version:
>>> 
>>> import Finney
>>> class Addable(Finnney.Immutable): pass
>>> Finney.Immutable = Addable
>>
>> Python's consenting adults philosophy allows the class designer some
>> limited ability to force the class user to think about it before messing
>> about with private variables. I think Ben's immutable class falls into
>> that same category.
> 
> If you read the whole thread, you'll see that I don't have a problem
> with messing with private variables. It's disallowing the addition of
> new variables that I found objectionable.

But you've just demonstrated that those who really want to shoot
themselves in the foot, er, add new attributes (not variables, let's get
the terminology right) can do so by sub-classing and rebinding. The fact
that Ben's proposal would prevent you from adding attributes is precisely
analogous to the name mangling that Python does: if you want to work
around it, it is easy to do so, but you have to make a conscious effort to
do so it.

"Hmmm, the class designer didn't want me adding attributes to instances...
maybe he had a good reason for that..."


-- 
Steven.




More information about the Python-list mailing list