Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #32433
| References | (1 earlier) <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 | 2012-10-29 15:45 -0700 |
| Subject | Re: Immutability and Python |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3052.1351550783.27098.python-list@python.org> (permalink) |
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