Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #59180

Re: 'isimmutable' and 'ImmutableNester'

References <CAB6+5b_w+kBT=EDqd9wRO80am+Wp2DgrEqSpVEPWkcTAVmYQtQ@mail.gmail.com> <1384206048.30461.46091021.634F0FCA@webmail.messagingengine.com>
Date 2013-11-12 12:10 +0100
Subject Re: 'isimmutable' and 'ImmutableNester'
From Frank-Rene Schäfer <fschaef@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.2453.1384254623.18130.python-list@python.org> (permalink)

Show all headers | View raw


> 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?

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

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

csiph-web