[Tutor] Iterating through a list of strings
spir ☣
denis.spir at gmail.com
Mon May 3 14:06:08 CEST 2010
On Mon, 03 May 2010 13:08:18 +0200
Stefan Behnel <stefan_ml at behnel.de> wrote:
> > Why aren't strings mutable, or lists immutable?
>
> What would be the use case of an immutable list, as opposed to a tuple? How
> would you use mutable strings in a dictionary?
[I'm not totally sure of the following, take it with some doubt.]
Lua does it in a rather different manner for its tables. Keys are compared by reference, not value. But immutable thingies are guaranteed to be unique. (Meaning eg there are no 2 objects equal to "abc".) This brings the following advantages:
* A unique hash func, and fast since the reference (address) is an integer.
* Any object can be a key, since the _reference_ is indeed immutable ;-).
Surprisingly, this works fine and does the right thing :-) When checking a key:
* If it's a value (mutable), unicity ensures that equality of reference also means equality of value.
* If it's a "thing" (immutable), like in python comparison in fact compares references.
The fact that the actual keys are references allows mutable objects to be used as keys. The price is indeed a lookup at immutable object creation (no idea how this is done, but I've heard it's efficient).
Denis
________________________________
vit esse estrany ☣
spir.wikidot.com
More information about the Tutor
mailing list