'isimmutable' and 'ImmutableNester'

Duncan Booth duncan.booth at invalid.invalid
Tue Nov 12 16:50:24 CET 2013

=?UTF-8?Q?Frank=2DRene_Sch=C3=A4fer?= <fschaef at gmail.com> wrote:

> The ImmutableNester special class type would be a feature to help
> checks to avoid recursion. Objects of classes derived from
> ImmutableNester have no mutable access functions and allow insertion
> of members only at construction time. At construction time it checks
> whether all entered elements are immutable in the above sense.

How does this help anything? If the objects are all immutable the object 
cannot contain any recursive references.

If you cannot see this think about tuples: a tuple containing immutable 
objects including other tuples can never contain a reference to itself 
because by definition the tuple did not exist at the point where the 
elements it contains were constructed.

Python already relies on the non-recursive nature of nested tuples when 
handling exceptions: The expression in the 'except' clause "is compatible 
with an exception if it is the class or a base class of the exception 
object or a tuple containing an item compatible with the exception".

If you try using something like a list in the exception specification you 
get a TypeError; only tuples and exception classes (subclasses of 
BaseException) are permitted. This means the structure can be as deeply 
nested as you wish, but can never be recursive and no checks against 
recursion need to be implemented.

Duncan Booth http://kupuguy.blogspot.com

More information about the Python-list mailing list