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


Groups > comp.lang.python > #59165

Re: 'isimmutable' and 'ImmutableNester'

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin3!goblin1!goblin2!goblin.stu.neva.ru!newsfeed1.swip.net!uio.no!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 <fschaef@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.007
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; "subject:' ": 0.07; 'caller': 0.09; 'consistency': 0.09; 'obj': 0.09; 'try:': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; '(1,': 0.16; 'curtain': 0.16; 'immutability': 0.16; 'members)': 0.16; 'mutable': 0.16; 'overriding': 0.16; 'referencing': 0.16; 'typeerror:': 0.16; 'cc:addr:python.org': 0.22; 'print': 0.22; 'string,': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'skip:p 30': 0.29; 'message-id:@mail.gmail.com': 0.30; "skip:' 10": 0.31; 'accomplished': 0.31; 'assert': 0.31; 'concern': 0.31; 'int,': 0.31; 'tuples': 0.31; 'class': 0.32; '(including': 0.33; 'not.': 0.33; 'copying': 0.34; 'skip:_ 10': 0.34; 'could': 0.34; 'except': 0.35; '(2)': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'described': 0.36; 'false': 0.36; 'behind': 0.37; 'itself': 0.39; 'skip:p 20': 0.39; 'tell': 0.60; 'skip:t 30': 0.61; 'from:charset:utf-8': 0.61; 'such': 0.63; 'more': 0.64; 'benefit': 0.68; 'special': 0.74; 'as:': 0.81; 'fragment': 0.84; 'risking': 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=DRV48O8nvUtKL4u73pRDVf+fkNl1mTori1hp6a+L+Ms=; b=gZ7NSlkxnu27voPrfjxOIL8iwqpW06nTsuvTAGSYQ8N9CAulta6P1SOMKs4TULaLeM 4NWBYqgbP0o9CnPyiEvwATGJklhBDmJvUQQih+pWLwE9fFVEcmTWIOF5mrNwuSpODLqp IM/OH71kR7G+5maYGcZUnJQvcbsypEpi72pb1ElmzCDR7XpaWVyn/8bJCOqCdd9tUlfT Xc7n7kVKlkysa8YrY7G03VcpubQ9q4edjkj3wEX910vhoyw+VtdMIqYf7w8cGmz45zaU 8hw1TTEC2QDUNMFTuHrX5oVH6LlzhrFI4ck9PxZChjpQfa6rVr2gYqtg0cJZdhBIZA3C T9ug==
MIME-Version 1.0
X-Received by 10.49.110.9 with SMTP id hw9mr54872177qeb.4.1384247542536; Tue, 12 Nov 2013 01:12:22 -0800 (PST)
In-Reply-To <1384206048.30461.46091021.634F0FCA@webmail.messagingengine.com>
References <CAB6+5b_w+kBT=EDqd9wRO80am+Wp2DgrEqSpVEPWkcTAVmYQtQ@mail.gmail.com> <1384206048.30461.46091021.634F0FCA@webmail.messagingengine.com>
Date Tue, 12 Nov 2013 10:12:22 +0100
Subject Re: 'isimmutable' and 'ImmutableNester'
From Frank-Rene Schäfer <fschaef@gmail.com>
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 <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.2439.1384247550.18130.python-list@python.org> (permalink)
Lines 68
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1384247550 news.xs4all.nl 15910 [2001:888:2000:d::a6]:36767
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:59165

Show key headers only | View raw


(1) hash()-ability != immutability (!)

Proof:

class X:
    def __hash__(self): return 0

def pseudo_isimmutable(this):
    try:
        hash(this)
        return True
    except TypeError:
        return False

shapeshifter = (1, 2, X())
print pseudo_isimmutable(shapeshifter)
shapeshifter[2].changed = 4711


(2) The intended scenario is not described by a fragment such as:

      if isimmutable(obj): x = obj
      else:                     x = copy.copy(obj)
      function_that_might_modify(x)

But instead, a more characteristic scenario is

     assert isimmutable(obj)
     # What happens behind the curtain may rely on referencing
     things_behind_the_curtain(obj)

Or,

    def let_me_know():
         obj = get_what_is_wanted()
         assert isimmutable(obj)
         # The caller may do with it what he wants without risking consistency
         return obj

where lots of copying





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?

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 | Next | Find similar | Unroll thread


Thread

Re: 'isimmutable' and 'ImmutableNester' Frank-Rene Schäfer <fschaef@gmail.com> - 2013-11-12 10:12 +0100

csiph-web