Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #32387 > unrolled thread
| Started by | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| First post | 2012-10-29 15:55 +0000 |
| Last post | 2012-10-29 15:55 +0000 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Immutability and Python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-10-29 15:55 +0000
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2012-10-29 15:55 +0000 |
| Subject | Re: Immutability and Python |
| Message-ID | <mailman.3022.1351525976.27098.python-list@python.org> |
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.
Back to top | Article view | comp.lang.python
csiph-web