Possible to create a read-only complex object?

Steven D'Aprano steve-REMOVE-THIS at cybersource.com.au
Mon Jul 12 04:42:05 EDT 2010


On Mon, 12 Jul 2010 01:11:53 -0700, Chris Rebert wrote:

> 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



Meh, defensive programming is designed to deal with bugs and hostile 
outsiders. I mean writing your functions and classes to deal with 
actively hostile coding *partners* who are trying to destroy your class 
so their class will survive to fight another day... 

There's probably a Dilbert cartoon about it.

I'm not serious of course, if the members of your project are trying to 
screw your code, you're in trouble. I just like the idea of competition-
driven development being the next buzzword, for when agile, test-driven, 
or pair-programming development is no longer enough.


-- 
Steven



More information about the Python-list mailing list