Immutability and Python

Chris Angelico rosuav at gmail.com
Mon Oct 29 12:08:31 EDT 2012


On Tue, Oct 30, 2012 at 2:55 AM, Paul Rubin <no.email at nospam.invalid> wrote:
> andrea crotti <andrea.crotti.0 at gmail.com> writes:
>> and we want to change its state incrementing the number ...
>>  the immutability purists would instead suggest to do this:
>>     def increment(self):
>>         return NumWrapper(self.number + 1)
>
> Immutability purists would say that numbers don't have "state" and if
> you're trying to change a number's state by incrementing it, that's not
> immutability.  You end up with a rather different programming style than
> imperative programming, for example using tail recursion (maybe wrapped
> in an itertools-like higher-order function) instead of indexed loops to
> iterate over a structure.

In that case, rename increment to next_integer and TYAOOYDAO. [1]
You're not changing the state of this number, you're locating the
number which has a particular relationship to this one (in the same
way that GUI systems generally let you locate the next and previous
siblings of any given object).

ChrisA
[1] "there you are, out of your difficulty at once" - cf WS Gilbert's "Iolanthe"



More information about the Python-list mailing list