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!newsfeed.xs4all.nl!newsfeed6.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'subject:Python': 0.05; 'modified': 0.05; 'class,': 0.07; 'suppose': 0.07; 'immutable': 0.09; 'mutable': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.10; 'suggest': 0.11; 'immutability': 0.16; 'instead:': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'wrote:': 0.17; 'code,': 0.18; 'example': 0.23; 'programming': 0.23; 'this:': 0.23; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; 'wonder': 0.27; 'header:X-Complaints-To:1': 0.28; 'this?': 0.28; 'skip:_ 10': 0.29; 'class': 0.29; 'classes': 0.30; 'normally': 0.30; 'sense': 0.31; 'from:addr:yahoo.co.uk': 0.32; 'comments': 0.33; 'doubt': 0.33; 'impression': 0.33; 'to:addr :python-list': 0.33; 'doing': 0.35; 'received:org': 0.36; 'but': 0.36; 'functional': 0.36; 'quite': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'mark': 0.38; 'object': 0.38; 'things': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'course.': 0.62; 'skip:n 10': 0.63; 'love': 0.63; 'more': 0.63; 'hand': 0.82; 'andrea': 0.84; 'beats': 0.84; 'number):': 0.84; 'why?': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: Immutability and Python Date: Mon, 29 Oct 2012 15:55:29 +0000 References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: host-92-24-117-50.ppp.as43234.net User-Agent: Mozilla/5.0 (Windows NT 6.0; rv:16.0) Gecko/20121010 Thunderbird/16.0.1 In-Reply-To: X-Antivirus: avast! (VPS 121029-0, 29/10/2012), Outbound message X-Antivirus-Status: Clean 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: 49 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1351525976 news.xs4all.nl 6984 [2001:888:2000:d::a6]:45962 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32387 On 29/10/2012 15:20, andrea crotti wrote: > I have a philosofical doubt about immutability, that arised while doing > the SCALA functional programming course. > > Now suppose I have a simple NumWrapper class, that very stupidly does: > > class NumWrapper(object): > def __init__(self, number): > self.number = number > > and we want to change its state incrementing the number, normally I > would do this > > def increment(self): > self.number += 1 > > > But the immutability purists would instead suggest to do this: > > def increment(self): > return NumWrapper(self.number + 1) > > > Now on one hand I would love to use only immutable data in my code, but > on the other hand I wonder if it makes so much sense in Python. > > My impression is that things get more clumsy in the immutable form, for > example in the mutable form I would do simply this: > > number = NumWrapper(1) > number.increment() > > while with immutability I have to do this instead: > new_number = number.increment() > > But more importantly normally classes are way more complicated than my > stupid example, so recreating a new object with the modified state might > be quite complex. > > Any comments about this? What do you prefer and why? > I prefer practicality beats purity. -- Cheers. Mark Lawrence.