Range Operation pre-PEP

Douglas Alan nessus at mit.edu
Sat May 12 01:06:41 EDT 2001


"Fredrik Lundh" <fredrik at pythonware.com> writes:

> In the dumbest subthread in quite a while, someone wrote:

Indeed, but I'm not the one making it dumb.

I made a *very* simple statement: tuples *are* full-fledged immutable
sequences in Python, and therefore it is fair and often useful to
treat them as full-fledged immutable sequences.

You chose to attack and insult me for this very straight-forward and
informed opinion.  That's pretty dumb, if you ask me.

> > Perhaps you should treat people who disagree with you with more
> > respect.

> when you deserve it.

And why don't I deserve it, Fredrik?

> > > if you're so experienced in Python as you say you are, maybe you can
> > > prove me wrong, by posting some code where you use lists and tuples
> > > the "wrong" way, and show that it won't work better if done the
> > > other way around.

> > I don't have to post any code

> didn't expect you to.  putting some weight behind your words isn't
> exactly your style.

Why should I?  No doubt you would just ridicule me and tell me how it
is "obvious" that your approach is better and what a dumbshit I must
be, because the superiority of your approach is a "fact".  It's better
to let Python speak for itself, since it belies your claims.

Face it, a tuple *is* a full-fledged sequence.  Therefore, it is just
not a "fact" that one should not use it as a full-fledged sequence and
only as a "record".  Sometimes immutable sequences are useful, as the
Python implementation proves.

> > I can just refer you to way Python itself works.  Why do excess
> > parameters get put into a tuple rather than a list?

> yeah, why?

> is it because tuples are immutable

In part.

> (which was your main argument until you decided to tilt at another
> windmill)?

What windmill is that?

> if so, how come python's using *mutable* dictionaries for keyword
> args?

Because, in part, there are no immutable dictionaries in Python.

> or is it because excess arguments are more important than ordinary
> argument lists, and thus have influenced the whole design?

I have no idea what you are talking about.  Who ever said that they
are "more" important?

> do you really believe that?

I have no idea what you are talking abut, so I can't say whether I
believe it or not.

> or is it because tuples are more efficient when the number of
> arguments is known at construction time (i.e when the function is
> called)?  if so, you just proved my point.

The number of elements for an immutable object must *always* be known
at construction time.  Otherwise it couldn't be immutable, now could
it?  But the determination of the size occurs at run-time.  A "record"
is something for which the length is typically known at compile time
(though in a dynamic language, such statements must always be taken
with a grain of salt).

Time efficiency of construction is indeed *one* of the benefits of
immutability.  It's far from the only one.  An immutable object will
also occupy less space.  It's semantics are less complicated and more
predictable.  Immutable objects are more amenable to certain
optimization techniques.

Another situation where you might want an immutable object is where
you want an object to reveal part of the internal state of its
representation for efficiency reasons, but it would be very bad if the
client modified it.  Using immutable objects in this case can help
reduce bugs and increase efficiency.  This is why dictionaries only
accept immutable objects as keys.  The same thing can be true if it
would be very bad if a called procedure were to modify the object.
Both of these situations are good reasons that strings are immutable.

Or consider this case: I want to use a sequence type to represent
polynomials of a single variable.  Such polynomials can be represented
as a sequence of numbers.  Consequently the sequence is homogeneous --
all numbers.  Each polynomial can be of a different length and they
are arbitrarily long.  Once a polynomial is created, it is never
changed.  Are you going to tell me, Fredrik, that such a polynomial is
better represented as a list than as a tuple?  What if I need to use
polynomials as dictionary keys?

> or maybe it's just an historical implementation artifact, and has
> nothing to do with the intended use of tuples vs. lists.

Are you saying that this use of tuples in Python is a "wart on the
language".  Be careful, Fredrik, you might be accused of being a
"troll".

> > Excess parameters are not of a fixed length, and they are more
> > typically homogeneous than heterogeneous

> oh, you really do believe that.

Of course I do.  Take this hypothetical function, for instance:

   lock("file1", "file2", "file3")

This either gets a lock on all the files or none of them.  This is
a pretty typical use of excess arguments.  Or just look at os.execl(),
os.execlp(), etc.

|>oug



More information about the Python-list mailing list