lists v. tuples
stephenhorne100 at aol.com
Mon Mar 17 14:06:59 CET 2008
On Mar 17, 12:28 pm, castiro... at gmail.com wrote:
> > Why is the immutable form the default?
> Using a house definition from some weeks ago, a tuple is a data
> structure such which cannot contain a refrence to itself. Can a
> single expression refer to itself ever?
Can't imagine why that feature was highlighted in particular, but a
list can reference itself even though an expression can't.
The following example looks a bit self-referential, but isn't...
a = 2
a = [1, a, 3] # result [1, 2, 3]
The following additional line, however does create a self-referencing
a  = a
The result being [1, [...], 2]
It's nice to see that Python can handle the output for this without
going into an infinite recursion - which is exactly what it used to do
in the distant past.
A tuple cannot be made to reference itself because it cannot be
modified after creation. The key point is that lists are mutable,
whereas tuples are not.
More information about the Python-list