Re: The Tuples and Lists question
Oren Tirosh <orent <at> hishome.net>
2008-06-11 15:46:43 GMT
On Wed, Jun 11, 2008 at 5:31 PM, Omer Zak <w1 <at> zak.co.il> 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.
Note that you are going against the current. Python is actually moving
in the direction of having more mutable and immutable variants of
types, not less:
set (mutable), frozenset (immutable)
And in py3k:
bytearray (mutable), bytes (immutable).
> Let's say that it has been decreed that a single data type serve as both
> Tuple and List. How would it be specified and implemented?
> First of all, let's use only square brackets  to represent literals.
> The parentheses () will be reserved for function and class method calls.
Tuples are constructed with COMMAS, not parentheses!
>>> x = 1, 2
>>> len( [[1,2,3]] )
>>> len( ((1,2,3)) )
Parentheses mean in Python exactly what they mean in math - order of
precedence. That's why two parens mean the same as one. Parens are
required for disambiguation in certain cases like passing a tuple to a
function but it's the comma that makes the tuple, not the parentheses
(with a special exception for the empty tuple).
> It may be possible to optionally prefix the brackets with a character
> signifying whether the data structure is mutable or not - like the r and
> u prefixes of string literals:
> m[1,2,3] - mutable List
> i["a","y","\u2034"] - immutable (would be Tuple in today's Python)
And would these prefixes create instances of a different types or the
same type with a mutability flag? If the former then you have just
changed one syntax for another, which was not your stated intent (type
unification). If the latter, then you have created something with no
precedent anywhere else in Python - a builtin type that magically
changes its behavior.
> > When you use a tuple, the fixed length carries the
> > implicit connotation that each position has a specific role "first is
> > x coordinate, second is y". "first is name, second is value", etc.
> If this is important, then use a class and give names to the positions.
If this was what I wanted I would be writing Java, not Python. A big
part of what I like about Python is its brevity. Not the perlesque
kind of brevity with mystic runes that makes the code less readable =
pythonic brevity makes the code is more readable and yet short enough
to fit into your screen and your mind. In Java you have to define so
many small helper classes that you often can't see the forest for the
Python-il mailing list
Python-il <at> hamakor.org.il