List vs tuples
Isaac To
kkto at csis.hku.hk
Sat Apr 10 23:09:36 EDT 2004
>>>>> "John" == John Roth <newsgroups at jhrothjr.com> writes:
John> The other major issue here is that you seem to be trying to argue
John> that tuples are unnecessary because lists can do everything that
John> tuples can, and more (except be used as keys in dicts, besides
John> they take up more memory and they're slower.)
Far from the truth (how can you imply what I think by just reading one
message I post, and that message didn't says anything that you believe I
"think"). I just find it strange that to argue that tuples is for
"emulating structs", which can be done perfectly (or indeed better) using
lists instead.
John> I don't buy that line of arguement. A large part of programming is
John> finding ways of expressing intent in the program so you don't
John> wonder what you did six months later. Tuples and lists serve such
John> different conceptual issues that they help expressiveness.
If lists and tuples are exactly (or nearly) the same, just like class and
struct in C++, then I'll buy the argument. Then the distinction would
simply be documentary (I believe C++ struct and class distinction serves
important purposes, too). But the fact is, lists and tuples are not the
same. If I have a program using lists and I suddenly change all of them to
tuples, or if I have a program using tuples and I suddenly change all of
them to lists, my program breaks. And it breaks in a way that resolving it
would cause substantial performance and correctness issues. If I need a
struct-like object and because of your argument, I use tuples, I'll get
burnt later by having to change all of them to lists and deal with the
performance and correctness issues.
So I don't buy that line of thoughts. Lists is something that can change,
and tuples is something that can't change (at least, the references it holds
can't change; if you change a reference referred by this reference that's
another matter). I think this by itself serves a very clear documentary
purpose, than the (what I believe bogus) argument that tuples serves as
something like a struct. So tuples looks like a "value", just like a string
looks like a value in Python; while lists looks like a "variable", just like
a dictionary.
Regards,
Isaac.
More information about the Python-list
mailing list