Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #32383

Immutability and Python

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <andrea.crotti.0@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*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; 'def': 0.10; 'suggest': 0.11; 'to:name:python-list': 0.15; 'immutability': 0.16; 'instead:': 0.16; 'code,': 0.18; 'received:209.85.214.174': 0.21; 'example': 0.23; 'programming': 0.23; 'this:': 0.23; 'wonder': 0.27; 'message-id:@mail.gmail.com': 0.27; 'this?': 0.28; 'skip:_ 10': 0.29; 'class': 0.29; 'classes': 0.30; 'normally': 0.30; 'sense': 0.31; 'comments': 0.33; 'doubt': 0.33; 'impression': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'doing': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'functional': 0.36; 'quite': 0.37; 'received:209': 0.37; 'data': 0.37; 'object': 0.38; 'things': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'header:Received:5': 0.40; 'course.': 0.62; 'skip:n 10': 0.63; 'love': 0.63; 'more': 0.63; 'hand': 0.82; 'number):': 0.84; 'why?': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=+lIx/rR6nbVeL3jLgAHvkm9f42DWx8ssprolOFYSOJQ=; b=VAt6K0eX7VhKHY5/BdCDVyiMvUr9g4JM4drm7lK/V6gvJoVKT4KKoDLrNpRysA+dcF X01K989awH6xbHXnumDbfdLUyvaeq177Eeatz5Fhndw69Sh35pdNTttdr24scSFfCa7A 4RJszLN3u1F6WrFV+spiiHWuHS4idP0TUL2w0RiQeivGZVkfQtyrg7kHKdeHzO7dlKmc EpYhIWVu+59cLfoiBEzcJsIBLrm03w53TuDeVSa9elrceswk7P3wEtgaGh5Py8mCI5pF WiDHVNt+jtM1dE7OgIabTqbmjIffXGMRx6nT00su+SFFkw+yDNYUQVSIbeGkt4h/Zodn Me7w==
MIME-Version 1.0
Date Mon, 29 Oct 2012 15:20:02 +0000
Subject Immutability and Python
From andrea crotti <andrea.crotti.0@gmail.com>
To python-list <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
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.3018.1351524004.27098.python-list@python.org> (permalink)
Lines 39
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1351524004 news.xs4all.nl 6854 [2001:888:2000:d::a6]:53288
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:32383

Show key headers only | View raw


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?

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Immutability and Python andrea crotti <andrea.crotti.0@gmail.com> - 2012-10-29 15:20 +0000
  Re: Immutability and Python Paul Rubin <no.email@nospam.invalid> - 2012-10-29 08:55 -0700
    Re: Immutability and Python Chris Angelico <rosuav@gmail.com> - 2012-10-30 03:08 +1100
    Re: Immutability and Python andrea crotti <andrea.crotti.0@gmail.com> - 2012-10-29 16:33 +0000
  Re: Immutability and Python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-29 23:02 +0000
  Re: Immutability and Python rusi <rustompmody@gmail.com> - 2012-10-30 09:22 -0700
    Re: Immutability and Python Neal Becker <ndbecker2@gmail.com> - 2012-10-30 16:45 -0400
      Re: Immutability and Python rusi <rustompmody@gmail.com> - 2012-10-30 20:21 -0700
  Re: Immutability and Python Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-11-08 08:38 +0100

csiph-web