make an object read only

dieter dieter at handshake.de
Wed Aug 3 03:57:42 EDT 2016


Robin Becker <robin at reportlab.com> writes:

> A reportlab user found he was doing the wrong thing by calling canvas.save 
> repeatedly, our documentation says you should not use Canvas objects after the 
> save method has been used. The user had mixed results :(
>
> It would be better to make the canvas object completely immutable all the way 
> down when save has been called, but I think that's quite hard as these objects 
> have quite a large and varied set of attributes, lists other objects 
> dictionaries etc etc.

If changing things happen at an elementary level (by assigning to
the object's instance attributes), then you could implement
your on "__setattr__/__delattr__" methods. There, you could check
whether you are ready to allow the change or reject it.




More information about the Python-list mailing list