Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!news2.euro.net!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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python,': 0.02; 'exception': 0.03; 'subject:Python': 0.05; 'overflow': 0.07; '__init__': 0.09; 'mutable': 0.09; 'tuple': 0.09; 'extensions': 0.13; 'stack': 0.15; 'filename:fname piece:signature': 0.16; 'immutability': 0.16; 'namedtuple': 0.16; 'tuple,': 0.16; 'wrote:': 0.17; 'saying': 0.18; 'discussion': 0.20; 'define': 0.20; 'meant': 0.21; 'references': 0.23; 'raise': 0.24; 'header :In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; '(which': 0.26; 'guess': 0.27; 'functions.': 0.27; 'really,': 0.29; 'though.': 0.29; 'skip:_ 10': 0.29; 'probably': 0.29; 'classes': 0.30; 'e.g.': 0.30; 'function': 0.30; 'error': 0.30; 'skip:_ 30': 0.32; 'to:addr:python-list': 0.33; 'another': 0.33; "can't": 0.34; 'done': 0.34; 'pm,': 0.35; 'really': 0.36; 'except': 0.36; 'but': 0.36; 'wanted': 0.36; 'anything': 0.36; 'should': 0.36; 'possible': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'sure': 0.38; 'gives': 0.39; 'to:addr:python.org': 0.39; 'short': 0.39; 'think': 0.40; 'your': 0.60; 'places': 0.61; "you'll": 0.62; 'worth': 0.63; 'times': 0.63; 'andrea': 0.84; 'answer:': 0.84; 'played': 0.84; 'hassle': 0.91; 'this;': 0.91 Date: Mon, 29 Oct 2012 12:58:58 -0500 From: Evan Driscoll User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.8) Gecko/20121012 Thunderbird/10.0.8 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Re: Immutability and Python References: <1793477354.3492917.1351526431192.JavaMail.root@sequans.com> <7x625t6xaj.fsf@ruckus.brouhaha.com> In-Reply-To: X-Enigmail-Version: 1.4 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="------------enigF885F85C80DCE2D4CFBC3B4E" 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: 53 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1351533554 news.xs4all.nl 6927 [2001:888:2000:d::a6]:58088 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32410 This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --------------enigF885F85C80DCE2D4CFBC3B4E Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On 10/29/2012 12:05 PM, 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.. The short answer is: you don't, not really, except by using NamedTuple if that gives you what you want. The longer answer: You can kinda get it somewhat if you define your own __getattribute__/__setattribute__ functions. __setattribute__ of course should never do anything except raise an error (one way or another you'll need to make an exception for your __init__ function of course). __getattribute__ should make sure no mutable references are returned: e.g. you'll probably want to make it so someone can't side-step your setter by saying someobject.__dict__["foo"] =3D "bar". (I return a copy o= f the dict.) It will still be possible to bypass these protections though. To really get true immutability in pure Python, you'll have to inherit from tuple or NamedTuple (which inherits from tuple, I think). You can see some discussion on Stack Overflow and some other places about this; having played around with this a bit, I think it's not worth the hassle and have done the __getattribute__/__setattribute__ thing the couple of times I wanted immutability. Evan --------------enigF885F85C80DCE2D4CFBC3B4E Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.14 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iQEcBAEBAgAGBQJQjsPoAAoJEAOzoR8eZTzgo2cH/jgg+Ht3FtDrAlswAV9P/pgo nHahvG/FwLkrAwvJB/YE15SMv31XdrALu3BYqfzeOJOTmKZx/QUy8/HeMzOIqJak /hwrvGOqDh+12CAhBI3jWiTup9jdROPkLonx31EmKy5EWgqKjTuSEbBZeKqXpxyX SjHjXlGDup20FRYb+gTmsJfDqmyctq++JL9yfaBytgh/LzRhHPjaY/keRDo4f8S1 GRT00H7xrkGsmxpRrUG0T4sbRX1CQ2FtmHb7lrmR+gpDt2F5iw6Cknx0Su5z6oFV gx2tzDhJ0JbpjxQcukT68IUrCBPeSBRecxpZGoNPNoIIFLjnYIIKrsCrKroz7pY= =QnP7 -----END PGP SIGNATURE----- --------------enigF885F85C80DCE2D4CFBC3B4E--