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


Groups > comp.lang.python > #59160

Re: 'isimmutable' and 'ImmutableNester'

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'tree': 0.05; 'lines,': 0.07; 'puts': 0.07; "subject:' ": 0.07; 'immutable': 0.09; 'insertion': 0.09; 'try:': 0.09; 'def': 0.12; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'immutability': 0.16; 'mutable': 0.16; 'objects.': 0.16; 'tuple': 0.16; 'typeerror:': 0.16; 'unhashable': 0.16; 'prevent': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'define': 0.26; 'first,': 0.26; 'somewhere': 0.26; 'header :In-Reply-To:1': 0.27; 'function': 0.29; 'message- id:@mail.gmail.com': 0.30; "skip:' 10": 0.31; '"",': 0.31; 'existence': 0.31; 'second,': 0.31; 'file': 0.32; '(most': 0.33; 'except': 0.35; 'objects': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'false': 0.36; 'members.': 0.37; 'implement': 0.38; 'nov': 0.38; 'to:addr:python- list': 0.38; 'pm,': 0.38; 'recent': 0.39; 'does': 0.39; '12,': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'how': 0.40; 'personal': 0.63; 'soon': 0.63; 'become': 0.64; 'more': 0.64; 'obvious': 0.74; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; bh=Zt1DCjINZWYTNIDHe6m6fz/B4hag72CAYlVFWK9e+tQ=; b=OxWV0tGxHUnKwQfLxMDYwGnfWYL+DTcL0ilZDvDqVI6qxQe5UZ6RAqnoEQPO8Q976I +/WDHlGWG9u1b21IMOOv4p9W8+CTsbh2vRmDT0PT36xRfasbw2CXZ1vDBhGt1ZtbdZLD 4Lkr5LJcP2Dtq/CsK1NsdxpavRUc5njVaijvkXl2jMccRiho6RQ8Ibp1uZ9AzZ7V85af ZVxnZQysrUpknSYbReQesPSz32mwmLFsY+jGr3FFUe3boJGTTwD9v06sd8zXHBkHnV2M lVyortOPWEYk3/VRS9qNCp8hF3BeUuWkf42q0qvSPKZvIlgEpme7OOeoh4aXTsu9fSKW GWGA==
MIME-Version 1.0
X-Received by 10.68.240.2 with SMTP id vw2mr34388712pbc.80.1384240363176; Mon, 11 Nov 2013 23:12:43 -0800 (PST)
In-Reply-To <CAB6+5b-KZE1xWqyefFicRkk4xhr9aqSWvaOhvgHJGf0s4RpexQ@mail.gmail.com>
References <CAB6+5b_w+kBT=EDqd9wRO80am+Wp2DgrEqSpVEPWkcTAVmYQtQ@mail.gmail.com> <1384206048.30461.46091021.634F0FCA@webmail.messagingengine.com> <CAB6+5b-KZE1xWqyefFicRkk4xhr9aqSWvaOhvgHJGf0s4RpexQ@mail.gmail.com>
Date Tue, 12 Nov 2013 18:12:43 +1100
Subject Re: 'isimmutable' and 'ImmutableNester'
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2431.1384240366.18130.python-list@python.org> (permalink)
Lines 35
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1384240366 news.xs4all.nl 15980 [2001:888:2000:d::a6]:35168
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:59160

Show key headers only | View raw


On Tue, Nov 12, 2013 at 6:01 PM, Frank-Rene Schäfer <fschaef@gmail.com> wrote:
> A tuple is immutable but it may contain mutable objects. In larger
> hierarchies of objects it may become less obvious whether down
> the lines, there is some mutable object somewhere in the data tree.
>
> One can define a recursive function to check for immutability
> manually. However first, it may not be as efficient as if it was
> built-in. Second, the existence of a built-in function 'isimmutable'
> puts the concept of immutability some more into the spotlight.
>
> You might indeed implement some personal 'policy for copy/deepcopy'.
> But, how can you prevent the insertion of an object into the data
> tree which does not follow your copy/deepcopy convention? As soon
> as you allow members of type 'tuple' you must either check recursively
> or only allow ints and strings as tuple members.

>>> x=1,2,3
>>> hash(x)
-378539185
>>> x=1,2,[3]
>>> hash(x)
Traceback (most recent call last):
  File "<pyshell#424>", line 1, in <module>
    hash(x)
TypeError: unhashable type: 'list'

There's your recursive function!

def isimmutable(x):
    try:
        hash(x)
        return True
    except TypeError:
        return False

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


Thread

Re: 'isimmutable' and 'ImmutableNester' Chris Angelico <rosuav@gmail.com> - 2013-11-12 18:12 +1100
  Re: 'isimmutable' and 'ImmutableNester' Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-12 11:14 +0000
    Re: 'isimmutable' and 'ImmutableNester' Robert Kern <robert.kern@gmail.com> - 2013-11-12 14:00 +0000

csiph-web