Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; "subject:' ": 0.07; 'derived': 0.09; 'immutable': 0.09; 'insertion': 0.09; 'cc:addr :python-list': 0.11; 'python': 0.11; 'def': 0.12; 'fits': 0.16; 'hashable': 0.16; 'members)': 0.16; 'mutable': 0.16; 'overriding': 0.16; 'str,': 0.16; 'elements': 0.16; 'entered': 0.20; 'not,': 0.20; 'cc:addr:python.org': 0.22; "aren't": 0.24; 'stick': 0.24; 'string,': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'function': 0.29; 'feature': 0.29; 'said,': 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; "skip:' 10": 0.31; 'accomplished': 0.31; 'are.': 0.31; 'concern': 0.31; 'int,': 0.31; 'tuples': 0.31; 'class': 0.32; 'figure': 0.32; '(including': 0.33; 'not.': 0.33; 'could': 0.34; 'knowledge': 0.35; 'classes': 0.35; 'something': 0.35; 'objects': 0.35; 'received:google.com': 0.35; 'false': 0.36; 'possible': 0.36; 'so,': 0.37; 'checks': 0.38; 'itself': 0.39; 'how': 0.40; 'tell': 0.60; 'from:charset:utf-8': 0.61; 'benefit': 0.68; 'construction': 0.72; 'special': 0.74; 'itself?': 0.84 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 :cc:content-type; bh=cApvAJiJQJ7iMTZXpPtj7GxJxwQaqopvXSyMYXXD1NI=; b=Ck5E+2UP93TKXNhV3dopXFW3FgwN+rY9d2+TIE5pKJJU64kcsvCfu3EXipTKQDUz5m vGtmZyQhxOOXwhuw0qnrBi9ZempbgqpEaBd36o1AOen8HErpK7sI9+s4XTP8QhVO4/m4 VDjdg9lUiwjXeZGIKn6wmf5PovROwEr6qTHPabdOyRAcSTp+vVCIn9g1EFJZ+Qgos9B9 90ioLbxN3aPufLwisiw/h099J39qsmP/rG69GGqboNviFJJud8wuPymqKN2lYtWsTtDT y5VmLH3wrfMqCeUNqa+AD6GFY761iCZRnQcGCygclwoBzrhgniHuPz86XZDyDkM7mHnb PqxA== MIME-Version: 1.0 X-Received: by 10.49.105.67 with SMTP id gk3mr35859835qeb.3.1384254621708; Tue, 12 Nov 2013 03:10:21 -0800 (PST) In-Reply-To: <1384206048.30461.46091021.634F0FCA@webmail.messagingengine.com> References: <1384206048.30461.46091021.634F0FCA@webmail.messagingengine.com> Date: Tue, 12 Nov 2013 12:10:21 +0100 Subject: Re: 'isimmutable' and 'ImmutableNester' From: =?UTF-8?Q?Frank=2DRene_Sch=C3=A4fer?= To: random832@fastmail.us Content-Type: text/plain; charset=UTF-8 Cc: python-list@python.org 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: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1384254623 news.xs4all.nl 15904 [2001:888:2000:d::a6]:39945 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:59180 > 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 : >> 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?