On Wed, Jun 11, 2008 at 05:31:12PM +0300, Omer Zak wrote:
> First of all, thanks to those who showed interest in the subject and
> replied to my question.
> 
> Please consider the following as an academic exercise.  Actually I am
> reasonably happy with the current way things are done in Python.  Only
> that I am curious to see whether Tuples and Lists could be combined into
> a single data type, with some additional behind the curtains processing.
...
> 
> To illustrate it further, let's follow the life cycle of a data
> structure.
>   x = [1.1,2.2,4.5,"e"]
> It is marked as mutable, as it has no reference which expects it to be
> immutable.
>   y = { x : "Emu", 42 : "Gnu" }
> Now, the value of x has a reference which expects it to be mutable and a
> reference which expects it to be immutable.  So the value is flagged as
> immutable.
>   x[1] += 0.1
> Since the value of x is marked as immutable, the above operation copies
> it and modifies the copy.  The value serving as hash key is now
> referenced only by the Dict (and stays immutable), and the value
> referenced by x is a new copy - and marked as mutable until and unless
> referenced by someone who expects it to be immutable.
>   z = x + [lambda v: v+1]
> Now, x refers to the [0:4] slice of the list, while z refers to the
> entire list. (NOTE: this point can be debated - do we alter the value of
> x or do we create a copy and assign to z a modified copy?)

What you're saying is that the "x[1] += 0.1" operation implicitly
assigns to x as a side effect, changing its reference to some new object. 
This is already confusing and unexpected. And what if this happens inside a 
subfunction that receives x[1]? It might work, but I suspect it would not be 
usable.

> > 3. Have both both tuples and lists, but have a different syntax for 
> > handling them than Python currently has.
> 
> I am not interested in such a trivial change.
>  
> > On Wed, 2008-06-11 at 16:34 +0300, Oren Tirosh wrote: 
> > Yes, the only technical difference is that tuples are immutable. For
> > me that's enough of a raison d'etre for tuples. Indexing dictionaries
> > by a combination of keys is quite useful. Sets of tuples even more. I
> > actually used that yesterday.
> 
> I agree that it is very useful to use sequences (Tuples/Lists) as Dict keys.  
> I myself use this technique.

I find myself wanting immutable dictionaries for this purpose...

>>> idict(var1=a, var1=a) = {"var1" : 1, "var2" : 2 }
_______________________________________________
Python-il mailing list
[email protected]
http://hamakor.org.il/cgi-bin/mailman/listinfo/python-il

לענות