Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed5.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; 'attribute': 0.05; 'caller': 0.07; 'python': 0.09; 'immutable': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'extensions': 0.13; "'b'": 0.16; '__slots__': 0.16; 'attributes.': 0.16; 'foot': 0.16; 'mutated': 0.16; 'oct': 0.16; 'shooting': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'mon,': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'otherwise,': 0.20; 'meant': 0.21; 'received:209.85.214.174': 0.21; '"",': 0.22; 'cc:2**0': 0.23; 'properties': 0.24; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; '(most': 0.27; 'guess': 0.27; 'message-id:@mail.gmail.com': 0.27; 'behaviour': 0.29; "d'aprano": 0.29; 'shoot': 0.29; 'steven': 0.29; 'definition': 0.29; 'class': 0.29; 'classes': 0.30; 'code': 0.31; 'file': 0.32; 'traceback': 0.33; 'themselves': 0.33; "can't": 0.34; 'received:google.com': 0.34; 'pm,': 0.35; 'received:209.85': 0.35; 'add': 0.36; 'but': 0.36; 'possible': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'received:209.85.214': 0.39; 'header:Received:5': 0.40; 'truly': 0.62; 'state,': 0.65; 'protect': 0.69; 'construction': 0.72; 'andrea': 0.84 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:x-gm-message-state; bh=vRoT1D6H+cRf8S02Ud5gNDvdAH6l0agkjeArk8boADw=; b=bSxMK8z/9/o8g6K+OU7H8v9XbJsc0ZnYcnY4Q2aXvhqOawFdQfwzKPqYIBrREn/jr7 HPB0X7NAj5XI4k3S1vxkGoapw7MeMKyVBskhSaX0xbXEQpM/pET92M3XfuddlGGMQxq+ JxNIxYiQFtr+fNJo2UnokWpWvPbbUazAXAgRSTinTMDv8Lb+alxFqHZgq+A/7vVhEYNE wotVFdltVVICcHsp5T0asx6PwpyqnMWpPOqMgfe19PiAJvFyI1BXJDerZfC3yF8k89eI U9y6Z6lq+3QlfAmm46ZjTH7noIAtXbOqowdHZMbnee8r9kKH1NFWi0IwLqojxdCdYRn6 Yp1w== MIME-Version: 1.0 In-Reply-To: <508f0390$0$29967$c3e8da3$5496439d@news.astraweb.com> References: <1793477354.3492917.1351526431192.JavaMail.root@sequans.com> <7x625t6xaj.fsf@ruckus.brouhaha.com> <508f0390$0$29967$c3e8da3$5496439d@news.astraweb.com> From: Chris Kaynor Date: Mon, 29 Oct 2012 15:45:59 -0700 Subject: Re: Immutability and Python To: "Steven D'Aprano" Content-Type: text/plain; charset=ISO-8859-1 X-Gm-Message-State: ALoCoQm007Bcust7jhgsjPviagTf3379rkBRRzNMsVvaqOVCNJyUT4NAmk51bT9dzVMOX1FdpKk6 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: 50 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1351550783 news.xs4all.nl 6936 [2001:888:2000:d::a6]:52899 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32433 On Mon, Oct 29, 2012 at 3:30 PM, Steven D'Aprano wrote: > On Mon, 29 Oct 2012 17:05:07 +0000, andrea crotti wrote: > >> I meant how do I create new immutables classes myself, I guess that's >> possible writing C extensions but I don't see in pure Python.. > > Well, you can't *quite* make a truly immutable class in pure-Python, > because if *your* Python code can manipulate the class during > construction then so can the caller's Python code after construction. > > The trivial way to make an immutable class in Python is to inherit from > an already immutable class and add behaviour but no state: > > class MyInt(int): > def inc(self): > return self.__class__(self + 1) > > > Otherwise, you can add private state and rely on the caller not shooting > themselves in the foot by accessing single-underscore names, use > properties to protect private state, etc. > You'd also need to add __slots__ = () to the class definition to make it immutable. Otherwise they still can shoot themselves in the foot by adding new attributes. >>> class MyInt(int): ... def inc(self): ... return self.__class__(self+1) ... >>> a = MyInt() >>> a.b = 1 # Oops. Mutated "a". >>> a.b 1 >>> class MyInt(int): ... __slots__ = () ... def inc(self): ... return self.__class__(self + 1) ... >>> a = MyInt() >>> a.b = 1 AttributeError: 'MyInt' object has no attribute 'b' Traceback (most recent call last): File "", line 1, in AttributeError: 'MyInt' object has no attribute 'b'