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: 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: References: <1384206048.30461.46091021.634F0FCA@webmail.messagingengine.com> Date: Tue, 12 Nov 2013 18:12:43 +1100 Subject: Re: 'isimmutable' and 'ImmutableNester' From: Chris Angelico 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Tue, Nov 12, 2013 at 6:01 PM, Frank-Rene Sch=E4fer w= rote: > 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=3D1,2,3 >>> hash(x) -378539185 >>> x=3D1,2,[3] >>> hash(x) Traceback (most recent call last): File "", line 1, in hash(x) TypeError: unhashable type: 'list' There's your recursive function! def isimmutable(x): try: hash(x) return True except TypeError: return False