Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #32433
| 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 | <ckaynor@zindagigames.com> |
| 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 | <CAF_E5Jbf0KJjDLV0jS-p_J9E4D8=_sPScgE+vkmkN2sMw=3aoA@mail.gmail.com> <1793477354.3492917.1351526431192.JavaMail.root@sequans.com> <mailman.3025.1351527152.27098.python-list@python.org> <7x625t6xaj.fsf@ruckus.brouhaha.com> <mailman.3031.1351530311.27098.python-list@python.org> <508f0390$0$29967$c3e8da3$5496439d@news.astraweb.com> |
| From | Chris Kaynor <ckaynor@zindagigames.com> |
| Date | Mon, 29 Oct 2012 15:45:59 -0700 |
| Subject | Re: Immutability and Python |
| To | "Steven D'Aprano" <steve+comp.lang.python@pearwood.info> |
| 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 <python-list.python.org> |
| List-Unsubscribe | <http://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 | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3052.1351550783.27098.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
On Mon, Oct 29, 2012 at 3:30 PM, Steven D'Aprano <steve+comp.lang.python@pearwood.info> 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 "<stdin-inspect>", line 1, in <module> AttributeError: 'MyInt' object has no attribute 'b'
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: Immutability and Python andrea crotti <andrea.crotti.0@gmail.com> - 2012-10-29 16:12 +0000
Re: Immutability and Python Paul Rubin <no.email@nospam.invalid> - 2012-10-29 09:46 -0700
Re: Immutability and Python andrea crotti <andrea.crotti.0@gmail.com> - 2012-10-29 17:05 +0000
Re: Immutability and Python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-29 22:30 +0000
Re: Immutability and Python Chris Kaynor <ckaynor@zindagigames.com> - 2012-10-29 15:45 -0700
Re: Immutability and Python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-29 23:14 +0000
Re: Re: Immutability and Python Evan Driscoll <driscoll@cs.wisc.edu> - 2012-10-29 12:58 -0500
Re: Immutability and Python Terry Reedy <tjreedy@udel.edu> - 2012-10-29 14:03 -0400
Re: Immutability and Python Devin Jeanpierre <jeanpierreda@gmail.com> - 2012-10-29 14:25 -0400
csiph-web