Possible to create a read-only complex object?

Chris Rebert clp2 at rebertia.com
Mon Jul 12 04:11:53 EDT 2010


On Mon, Jul 12, 2010 at 12:45 AM, Steven D'Aprano
<steve-REMOVE-THIS at cybersource.com.au> wrote:
> On Mon, 12 Jul 2010 02:56:34 -0400, Terry Reedy wrote:
>> On 7/11/2010 12:51 PM, python at bdurham.com wrote:
>>> I have a complex object with attributes that contain lists, sets,
>>> dictionaries, and other objects. The lists and dictionaries may
>>> themselves contain complex objects.
>>> I would like to provide a read-only version of this type of object for
>>> other developers to query for reporting. Is there a way to prevent
>>> other developers from changing the attributes of my complex and nested
>>> object?
>>> In researching this question, I have identified __setattr__ and
>>> __delattr__ as possible ways to prevent changes to simple attributes,
>>> but I don't believe these magic methods will prevent others from
>>> fiddling with attributes containing lists and dictionaries or the
>>> contents of these lists and dictionaries.
>>
>> Python was not really not developed for multi-developer projects whose
>> members are willing to stomp on each others objects.
>
> I like the idea of competition-driven development, where the code that
> survives best in the face of hostile developers gets used.

http://en.wikipedia.org/wiki/Defensive_programming

Cheers,
Chris



More information about the Python-list mailing list