Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #32935
| From | Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Immutability and Python |
| Date | 2012-11-08 08:38 +0100 |
| Organization | A newly installed InterNetNews server |
| Message-ID | <k7fni3$oml$1@r03.glglgl.gl> (permalink) |
| References | <mailman.3018.1351524004.27098.python-list@python.org> |
Am 29.10.2012 16:20 schrieb andrea crotti:
> 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.
You can have both. Many mutable types distinguish between them with
their operators.
To pick up your example,
class NumWrapper(object):
def __init__(self, number):
self.number = number
def __iadd__(self, x):
self.number += x
return self
def __add__(self, x):
return NumWrapper(self.number + x)
So with
number += 1
you keep the same object and modify it, while with
number = number + 1
or
new_number = number + 1
you create a new object.
> 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?
That's why I generally prefer mutable objects, but it can depend.
Thomas
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll 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