Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Kaynor Newsgroups: comp.lang.python Subject: Re: def __init__(self): Date: Tue, 26 Apr 2016 10:13:40 -0700 Lines: 52 Message-ID: References: <34e51ef5-9679-40ec-bc8f-47981353e9d7@googlegroups.com> <571F1361.3010402@digipen.edu> <85eg9sn7qo.fsf@benfinney.id.au> <87lh40pyik.fsf@elektro.pacujo.net> <571f9362$0$1602$c3e8da3$5496439d@news.astraweb.com> <1461687967.2507874.590200129.102244C8@webmail.messagingengine.com> <571f9836$0$1619$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de yqKZlYtwxLdHa9u3ySoLWwOzKvKT6G7+pYYE2FVJLr7w== Return-Path: 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; '(1,': 0.09; 'immutable': 0.09; 'obj': 0.09; 'python': 0.10; 'def': 0.13; 'suggest': 0.15; 'wed,': 0.15; '>>>': 0.15; 'properly': 0.15; "'b',": 0.16; '2016': 0.16; '6.0': 0.16; 'ctypes.': 0.16; 'immutable,': 0.16; 'object()': 0.16; 'reason.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subclassing': 0.16; 'subject:def': 0.16; 'wrote:': 0.16; '>': 0.18; 'email addr:gmail.com>': 0.18; '>>>': 0.20; 'to:name:python-list@python.org': 0.20; 'ctypes': 0.22; 'am,': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'chris': 0.26; 'compatible': 0.27; 'message-id:@mail.gmail.com': 0.27; 'object,': 0.27; 'index,': 0.29; 'other,': 0.29; 'class.': 0.30; 'probably': 0.31; 'anybody': 0.32; "d'aprano": 0.33; 'steven': 0.33; 'tue,': 0.34; 'this?': 0.34; 'received:google.com': 0.35; 'skip:c 30': 0.35; 'something': 0.35; 'but': 0.36; 'received:209.85': 0.36; 'possible': 0.36; '(and': 0.36; 'apple': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'skip:& 10': 0.37; 'wanted': 0.37; 'things': 0.38; 'doing': 0.38; 'received:209': 0.38; 'mean': 0.38; 'means': 0.39; 'to:addr:python.org': 0.40; 'back': 0.62; 'more': 0.63; 'skip:\xc2 10': 0.67; 'spend': 0.67; '26,': 0.72; 'jul': 0.72; 'obvious': 0.76; '4.2.1': 0.84; 'casting': 0.84; 'subject:self': 0.84; 'difficult,': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zindagigames-com.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=DhBWJ5d/pKo4g5RWaEACsi0aceDVR4Ty+cU2TGMnF/Q=; b=fEfZXnls3gnZdC4oIkhM5KQFWVSUuWNXR2upN8+lNojLP/IpuRL4cPDoxpq6hYiwT+ 6yp6L4sZItIcrWlKKj4w/G+FdlRbJykTTaf5tHLvXLialhiKw4BXt0t8g6lE+4SudmcI Gm+X/X/xdeFeLsDC5ifurrt5p0uhwI63ddoGfYEVGq7fz2CNIeN2hMLQKWcM4zRt73cD 6aP8Dbe5LWAKHs/rHvxApU6rGsW64Rlp9lLJqNShOnZzaQOnjCZ/CDFrVrS1Vnb+U4uD CU6bEE/lkc3IB0LPpOIyEZ5aZ2/6T0HbXi0q7Mvbv7XtjyF3V1ScOIB7Z1nHPPnpuBWN KgXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=DhBWJ5d/pKo4g5RWaEACsi0aceDVR4Ty+cU2TGMnF/Q=; b=h6xRpb53VPrEWSSNFob3zsMWRX25FGexuyIVu0SQF4yhGXsWAJ7POK0bfma3RGDXG+ tXbgG8BAclnwqvDHL49qJ+zHOocciaT402eUK9DE0S/jDhOqwin7Wt/j+AjTMZcM2cGV 0YvNGyXoS4b2EhdJ11okT54kDCFiO4l5V0OKsLaGvDAAcpm2nAn/w9lNzfzQUkworDWB 5Mx2p4z7SXIUBhWnvPQOoOmeKt4+lF2oPFRqGQaTkPwDwC/fWnqal4objbNhDgxxyhsB O9iW3iNqRnLGf0bgEWW4/bWFoe9ejTTh12OwbrURt/tyCdNPe6MCZoXtqyZwRYWV/Iqa AV9A== X-Gm-Message-State: AOPr4FWMWnpGaNq6AYjWW2Ho/PFZ8pWcYcRFKzLC22eff0Yhioz0I7CKpjfKLDvJCNkbavUg+2KExIp+qYohxA== X-Received: by 10.159.34.11 with SMTP id 11mr1929113uad.68.1461690841635; Tue, 26 Apr 2016 10:14:01 -0700 (PDT) In-Reply-To: X-Content-Filtered-By: Mailman/MimeDel 2.1.22 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <34e51ef5-9679-40ec-bc8f-47981353e9d7@googlegroups.com> <571F1361.3010402@digipen.edu> <85eg9sn7qo.fsf@benfinney.id.au> <87lh40pyik.fsf@elektro.pacujo.net> <571f9362$0$1602$c3e8da3$5496439d@news.astraweb.com> <1461687967.2507874.590200129.102244C8@webmail.messagingengine.com> <571f9836$0$1619$c3e8da3$5496439d@news.astraweb.com> Xref: csiph.com comp.lang.python:107676 On Tue, Apr 26, 2016 at 10:04 AM, Chris Angelico wrote: > On Wed, Apr 27, 2016 at 2:59 AM, Chris Kaynor > wrote: > > On Tue, Apr 26, 2016 at 9:32 AM, Steven D'Aprano > > wrote: > > > >> Subclassing immutable built-ins is the most obvious and simple (and > >> probably > >> common) way to get an immutable class. Actually immutable, short of > doing > >> wicked things with ctypes. > >> > > > > By wicked things with ctypes, do you mean something like this? By no > means > > do I suggest this actually be used by anybody for any reason. > > > > Tested with '2.7.10 (default, Jul 14 2015, 19:46:27) \n[GCC 4.2.1 > > Compatible Apple LLVM 6.0 (clang-600.0.39)]' > > > > import ctypes > > def changeTuple(tuple, index, newValue): > > obj = ctypes.cast(id(tuple), ctypes.POINTER(ctypes.c_long)) > > obj[3+index] = id(newValue) > > > >>>> a = ('a','b','c') > >>>> changeTuple(a, 0, 1) > >>>> a > > (1, 'b', 'c') > >>>> changeTuple(a, 1, 3) > >>>> a > > (1, 3, 'c') > > Yeah. By the look of things, you've just destroyed the reference counts. > > >>> a = ('a','b','c') > >>> b = object() > >>> changeTuple(a, 0, b) > >>> a > (, 'b', 'c') > >>> del b > >>> a > Segmentation fault > Yah, if you really wanted to make it work properly, you'd need to incref the newValue, while decref the oldValue. The incref would not be that difficult, but the decref would be more challenging, as you may have to also destroy the old object, though that might be possible by casting it back to a python object without the incref,. One way or the other, I did not exactly spend a ton of time to make it work properly :)