Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'author:': 0.03; 'tree': 0.05; 'class,': 0.07; 'nested': 0.07; 'skip:` 10': 0.07; "subject:' ": 0.07; 'derived': 0.09; 'immutable': 0.09; 'implemented.': 0.09; 'pep': 0.09; 'used.': 0.09; 'assume': 0.14; '``x``': 0.16; 'aggregates': 0.16; 'clone': 0.16; 'components.': 0.16; 'immutability': 0.16; 'mutable': 0.16; 'objects.': 0.16; 'provision': 0.16; 'rationale': 0.16; 'requires:': 0.16; 'subclasses': 0.16; 'text/x-rst': 0.16; 'elements': 0.16; 'thoughts': 0.19; 'manual': 0.22; 'necessary.': 0.24; 'skip:` 20': 0.24; 'idea': 0.28; 'point': 0.28; 'function': 0.29; 'wondering': 0.29; 'raise': 0.29; 'change,': 0.30; 'draft': 0.30; 'message- id:@mail.gmail.com': 0.30; 'gives': 0.31; 'code': 0.31; "skip:' 10": 0.31; 'concern': 0.31; 'once,': 0.31; 'purely': 0.31; 'safely': 0.31; 'class': 0.32; 'checked': 0.32; 'not.': 0.33; 'copying': 0.34; 'classes': 0.35; 'objects': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'consist': 0.36; 'functions.': 0.36; 'i.e.': 0.36; 'reflect': 0.36; 'version:': 0.36; 'example,': 0.37; 'list': 0.37; 'performance': 0.37; 'checks': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'called': 0.40; 'manually': 0.60; 'tell': 0.60; 'impact': 0.61; 'from:charset:utf-8': 0.61; 'further': 0.61; 'therefore,': 0.64; 'status:': 0.68; 'safe': 0.72; 'therefore': 0.72; 'construction': 0.72; 'special': 0.74; "'true'": 0.84; '2.7.1': 0.84; '============': 0.84; 'fragment': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=ErKJ+/TPlSYHQ+hTVvrwx91BYkso3UWKBnk0Uc9aK+0=; b=LVTOJpWP6oAFPq8NVQI6X7Bnq3uwkfNo5ysvPE/0N1xwFY9dxUHo5MiGGE9QSRxUKU mJc1KOGbsiWZnGn7ScQ0s+EEX1TWU++YxDWaipDlN5Icl8CBW5ScQYSMqbIGsveNTN0R oRvQvZUvgZK9Xv8SFwbumDCY2tNQF8GVFXGJpKcGVYQiQGsWPn8O+RCicolnbMJzLpe+ SvnQwZFubwpZiGfYK5qDSPjzHiBS+pZtL+L4IKKXIky5Zu/rW7mTCOahPglOZHaXwNIb hx+mZdpK1UXROfiZ20vJFLfY6IrLspatt2+DKLxGVkTwl7J7Oo5wi48e4k4knBW3eZnl yWdw== MIME-Version: 1.0 X-Received: by 10.229.137.69 with SMTP id v5mr29013028qct.4.1384202865417; Mon, 11 Nov 2013 12:47:45 -0800 (PST) Date: Mon, 11 Nov 2013 21:47:45 +0100 Subject: 'isimmutable' and 'ImmutableNester' From: =?UTF-8?Q?Frank=2DRene_Sch=C3=A4fer?= To: python-list@python.org Content-Type: text/plain; charset=UTF-8 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: 70 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1384202874 news.xs4all.nl 15892 [2001:888:2000:d::a6]:43863 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:59099 I prepared a PEP and was wondering what your thoughts are about it: PEP: Title: ``isimmutable(Obj)`` and/or ``ImmutableNester`` Version: Last-Modified: Author: Frank-Rene Schaefer, fschaef@users.sourceforge.net * BDFL-Delegate: * Discussions-To: fschaef@users.sourceforge.net Status: Draft Type: * Content-Type: text/x-rst * Requires: Created: 11-nov-2013 * Python-Version: 2.7.1 Post-History: * Replaces: * Superseded-By: * Resolution: General Idea ============ A built-in function 'isimmutable()' shall tell efficiently whether the object of concern is mutable or not. That is it must reflect on the whole object tree whether it contains mutable elements or not. For example, in the code fragment :: verdict_0 = isimmutable(3.14) verdict_1 = isimmutable((1,2,3)) verdict_2 = isimmutable(((1,2),(2,3),(3,4))) all verdicts are 'True' because the tested objects consist of purely immutable components. However, the ``x`` in :: x = (1,(2,"abc", [1,2,3])) verdict = isimmutable(x) triggers the verdict to be 'False' because ``x[1][2]`` is a list and therefore mutable. It may be conceivable to have a special class-type called ``ImmutableNester`` which has no write-access member functions and does not allow its derived classes to have write-access member functions. Instead, any derived class aggregates members at the time of construction. At this point in time, when members are nested in the class, it is checked if the members are of subclasses of ``ImmutableNester``. The advantage of the ``isimmutable()`` function is that no extra class functionality needs to be implemented. The disadvantage is that the immutability must be checked manually and at each time the object is used. The ``ImmutableNester`` class type checks for immutability once, at construction time and no further manual checks are necessary. Rationale ========= If an object is immutable then copying of it can be safely be replaced by a setting of a reference. The principal scenario is when an instance A gives an instance B access to some data D under the provision that B does not change it. Therefore, B must either clone the data or it must be safe to assume that the data cannot change, i.e. is immutable. If the objects are large and/or many there a significant performance impact may raise from a deepcopy or manual cloning of objects. Therefore, the ``isimmutable()`` built-in function is key for a safe implementation of reference-instead-of-copying. Ensuring immutability is also key for the so called 'Flyweight Design Pattern'.