Re: Immutability and Python

2012-11-07 Thread Thomas Rachel
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,

Re: Immutability and Python

2012-10-30 Thread rusi
On Oct 31, 1:45 am, Neal Becker wrote: > rusi wrote: > > On Oct 29, 8:20 pm, andrea crotti wrote: > > > >> Any comments about this? What do you prefer and why? > > > Im not sure how what the 'prefer' is about -- your specific num > > wrapper or is it about the general question of choosing mutabl

Re: Immutability and Python

2012-10-30 Thread Neal Becker
rusi wrote: > On Oct 29, 8:20 pm, andrea crotti wrote: > >> Any comments about this? What do you prefer and why? > > Im not sure how what the 'prefer' is about -- your specific num > wrapper or is it about the general question of choosing mutable or > immutable types? > > If the latter I would

Re: Immutability and Python

2012-10-30 Thread rusi
On Oct 29, 8:20 pm, andrea crotti wrote: > Any comments about this? What do you prefer and why? Im not sure how what the 'prefer' is about -- your specific num wrapper or is it about the general question of choosing mutable or immutable types? If the latter I would suggest you read http://en.wi

Re: Immutability and Python

2012-10-29 Thread Steven D'Aprano
On Mon, 29 Oct 2012 15:45:59 -0700, Chris Kaynor wrote: > On Mon, Oct 29, 2012 at 3:30 PM, Steven D'Aprano > wrote: >> On Mon, 29 Oct 2012 17:05:07 +, andrea crotti wrote: >> >>> I meant how do I create new immutables classes myself, I guess that's >>> possible writing C extensions but I don'

Re: Immutability and Python

2012-10-29 Thread Steven D'Aprano
On Mon, 29 Oct 2012 15:20:02 +, andrea crotti wrote: > I have a philosofical doubt about immutability, that arised while doing > the SCALA functional programming course. "Philosophical". Like most words derived from the ancient Greeks, the "F" sound uses "ph" rather than "f". > Now suppose

Re: Immutability and Python

2012-10-29 Thread Chris Kaynor
On Mon, Oct 29, 2012 at 3:30 PM, Steven D'Aprano wrote: > On Mon, 29 Oct 2012 17:05:07 +, andrea crotti wrote: > >> I meant how do I create new immutables classes myself, I guess that's >> possible writing C extensions but I don't see in pure Python.. > > Well, you can't *quite* make a truly i

Re: Immutability and Python

2012-10-29 Thread Steven D'Aprano
On Tue, 30 Oct 2012 06:36:52 +1100, Chris Angelico wrote: > On Tue, Oct 30, 2012 at 6:23 AM, Ian Kelly > wrote: >> _MyImmutableClass = namedtuple('MyImmutableClass', 'field1 field2 >> field3 field4') >> >> class MyImmutableClass(_MyImmutableClass): > > Question: Is it clearer to take advantage o

Re: Immutability and Python

2012-10-29 Thread Steven D'Aprano
On Mon, 29 Oct 2012 17:05:07 +, andrea crotti wrote: > I meant how do I create new immutables classes myself, I guess that's > possible writing C extensions but I don't see in pure Python.. Well, you can't *quite* make a truly immutable class in pure-Python, because if *your* Python code can

Re: Immutability and Python

2012-10-29 Thread Ian Kelly
On Mon, Oct 29, 2012 at 1:36 PM, Chris Angelico wrote: > Question: Is it clearer to take advantage of the fact that the base > class can be an arbitrary expression? > > class MyImmutableClass(namedtuple('MyImmutableClass', 'field1 field2 > field3 field4')): > > You lose the unnecessary temporary a

Re: Immutability and Python

2012-10-29 Thread Chris Angelico
On Tue, Oct 30, 2012 at 6:23 AM, Ian Kelly wrote: > _MyImmutableClass = namedtuple('MyImmutableClass', 'field1 field2 > field3 field4') > > class MyImmutableClass(_MyImmutableClass): Question: Is it clearer to take advantage of the fact that the base class can be an arbitrary expression? class M

Re: Immutability and Python

2012-10-29 Thread Ian Kelly
On Mon, Oct 29, 2012 at 10:12 AM, andrea crotti wrote: > Also because how doi I make an immutable object in pure Python? I sometimes use namedtuples for this. from collections import namedtuple MyImmutableClass = namedtuple('MyImmutableClass', 'field1 field2 field3 field4') If you want default

Re: Immutability and Python

2012-10-29 Thread Devin Jeanpierre
On Mon, Oct 29, 2012 at 12:46 PM, Paul Rubin wrote: > andrea crotti writes: >> Also because how doi I make an immutable object in pure Python? > > Numbers in Python are already immutable. What you're really looking for > is a programming style where you don't bind any variable more than once. N

Re: Immutability and Python

2012-10-29 Thread Terry Reedy
On 10/29/2012 1:05 PM, andrea crotti wrote: I meant how do I create new immutables classes myself, I guess that's possible writing C extensions but I don't see in pure Python.. If you mean class with immutable instances, mutate new instances in __new__ instead of __init__ and write a custom .

Re: Re: Immutability and Python

2012-10-29 Thread Evan Driscoll
On 10/29/2012 12:05 PM, andrea crotti wrote: > I meant how do I create new immutables classes myself, I guess that's > possible writing C extensions but I don't see in pure Python.. The short answer is: you don't, not really, except by using NamedTuple if that gives you what you want. The longer

Re: Immutability and Python

2012-10-29 Thread Terry Reedy
On 10/29/2012 11:20 AM, andrea crotti wrote: I have a philosofical doubt about immutability, that arised while doing the SCALA functional programming course. In real life, the physical world, things have mutable state, at least down to the atomic level. Do you only want to model mathematical w

Re: Immutability and Python

2012-10-29 Thread Paul Rubin
andrea crotti writes: > Also because how doi I make an immutable object in pure Python? Numbers in Python are already immutable. What you're really looking for is a programming style where you don't bind any variable more than once. This gives rise to a programming style that Python can support

Re: Immutability and Python

2012-10-29 Thread andrea crotti
2012/10/29 Chris Angelico : > On Tue, Oct 30, 2012 at 2:55 AM, Paul Rubin wrote: >> andrea crotti 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

Re: Immutability and Python

2012-10-29 Thread andrea crotti
2012/10/29 Jean-Michel Pichavant : > > > In an OOP language num.increment() is expected to modify the object in place. > So I think you're right when you say that functional languages technics do > not necessarily apply to Python, because they don't. > > I would add that what you're trying to sugg

Re: Immutability and Python

2012-10-29 Thread Chris Angelico
On Tue, Oct 30, 2012 at 2:55 AM, Paul Rubin wrote: > andrea crotti 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 puris

Re: Immutability and Python

2012-10-29 Thread Jean-Michel Pichavant
- Original Message - > 2012/10/29 Jean-Michel Pichavant : > > > > "return NumWrapper(self.number + 1) " > > > > still returns a(nother) mutable object. > > > > So what's the point of all this ? > > > > JM > > > > Well sure but it doesn't modify the first object, just creates a new > one.

Re: Immutability and Python

2012-10-29 Thread Paul Rubin
andrea crotti 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 tr

Re: Immutability and Python

2012-10-29 Thread Mark Lawrence
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): s

Re: Immutability and Python

2012-10-29 Thread andrea crotti
2012/10/29 andrea crotti : >> > > Well sure but it doesn't modify the first object, just creates a new > one. There are in general good reasons to do that, for example I can > then compose things nicely: > > num.increment().increment() > > or I can parallelize operations safely not caring about th

Re: Immutability and Python

2012-10-29 Thread andrea crotti
2012/10/29 Jean-Michel Pichavant : > > "return NumWrapper(self.number + 1) " > > still returns a(nother) mutable object. > > So what's the point of all this ? > > JM > Well sure but it doesn't modify the first object, just creates a new one. There are in general good reasons to do that, for examp

Re: Immutability and Python

2012-10-29 Thread Jean-Michel Pichavant
- Original Message - > 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): >