On Feb 26, 2012 1:35 AM, "Masklinn" <masklinn@masklinn.net> wrote:
>
> On 2012-02-26, at 00:05 , Steven D'Aprano wrote:
> > - Immutable types can be used as keys in dicts.
>
> *technically*, you can use mutable types as dict keys if you define
> their __hash__ no? That is of course a bad idea when the instances
> are *expected* to be modified, but it should "work".
I wouldn't say this is necessarily a bad thing at all. It just depends what defines the object. If an instance represent a specific object (e.g. a database record) you wouldn't expect the hash to change if you modified an attribute of it, since the instance still represents the same object.
>
> > - Immutable types protect you from errors. While you might intend not
> > to modify a data structure, bugs do happen.
>
> Immutables are also inherently thread-safe (since thread safety is about
> shared state, and shared immutables are not state). Which is a nice
> guarantee.
>
> > Python has excellent support for read-only data structures, so long as you write them in C.
>
> There's also good support of the "consenting adults" variety (use
> _-prefixed attributes for the actual state and expose what needs to be
> exposed via properties and methods). That can be simplified with a
> custom descriptor type which can only be set once (similar to java's
> `final`), it would be set in the type's constructor and never re-set
> from this.
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas@python.org
> http://mail.python.org/mailman/listinfo/python-ideas
Maybe I'm missing something here, but what's wrong with just using __getattr__, __setattr__ and __delattr__ to restrict access?