Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #59180 > unrolled thread
| Started by | Frank-Rene Schäfer <fschaef@gmail.com> |
|---|---|
| First post | 2013-11-12 12:10 +0100 |
| Last post | 2013-11-12 15:50 +0000 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: 'isimmutable' and 'ImmutableNester' Frank-Rene Schäfer <fschaef@gmail.com> - 2013-11-12 12:10 +0100
Re: 'isimmutable' and 'ImmutableNester' Duncan Booth <duncan.booth@invalid.invalid> - 2013-11-12 15:50 +0000
| From | Frank-Rene Schäfer <fschaef@gmail.com> |
|---|---|
| Date | 2013-11-12 12:10 +0100 |
| Subject | Re: 'isimmutable' and 'ImmutableNester' |
| Message-ID | <mailman.2453.1384254623.18130.python-list@python.org> |
> So how do you figure out whether something's immutable or not? Are you
> going to ask the object itself? If so, stick with __hash__, and just
> follow the rule that mutable objects aren't hashable - which is, if
> I'm not mistaken, how things already are. And if not, then how? How
> will you know if something has mutator methods?
Admittedly, I have no knowledge about the python implementation. A possible
way would be to say:
def isimmutable(this):
if isinstance(this, tuple):
for x in this:
if not isimmutable(x): return False
return True
return isisintance(this, (int, str, ImmutableNester))
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.
As said, I have no idea how much this fits into the general python
implementation.
2013/11/11 <random832@fastmail.us>:
>> A built-in function 'isimmutable()' shall tell efficiently whether the
>> object
>> of concern is mutable or not.
>
> What's the benefit over attempting to hash() the object?
>
> copy.deepcopy already has special case for int, string, and tuples
> (including tuples that do and do not have mutable members) - could what
> you need be accomplished by overriding __copy__ and __deepcopy__ in your
> custom class to return itself if it is immutable?
[toc] | [next] | [standalone]
| From | Duncan Booth <duncan.booth@invalid.invalid> |
|---|---|
| Date | 2013-11-12 15:50 +0000 |
| Message-ID | <XnsA276A0A82FF43duncanbooth@127.0.0.1> |
| In reply to | #59180 |
=?UTF-8?Q?Frank=2DRene_Sch=C3=A4fer?= <fschaef@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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web