Python Interview Questions
steve+comp.lang.python at pearwood.info
Tue Nov 20 00:53:16 CET 2012
On Mon, 19 Nov 2012 09:59:19 -0500, Roy Smith wrote:
> OK, I've just read back over the whole thread. I'm really struggling to
> understand what point you're trying to make. I started out by saying:
>> Use a list when you need an ordered collection which is mutable (i.e.
>> can be altered after being created). Use a tuple when you need an
>> immutable list (such as for a dictionary key).
> To which you obviously objected. So now you write:
>> I think a tuple is an immutable sequence of items, and a list is a
>> mutable sequence of items.
> So how is that different from what I said? Is this whole argument
> boiling down to your use of "immutable sequence" vs. my use of
> "immutable list"?
Sheesh, of course not. Give me some credit.
I gave some examples of when somebody might use lists, tuples, sets and
dicts. Apparently I forgot a couple, and you responded with a sarcastic
comment about the "One True Church Of Pythonic Orthodoxy And Theoretical
Correctness" and gave a couple of additional examples.
Although I didn't come out and *explicitly* say "I agree" to your
examples, I actually did, with one proviso: your example of using an
"immutable list" as dict key. So I asked a question about that *specific*
Under what sort of circumstances would somebody want to take a mutable
list of data, say a list of email addresses, freeze it into a known state,
and use that frozen state as a key in a dict? What would be the point?
Even if there was some meaningful reason to look up "this list of 12000
email addresses" as a single key, it is going to get out of sync with the
actual mutable list.
Your reply was to give your stack trace script as an example. That's a
fine example as a use-case for a temporary list, and I've done similar
things dozens, hundreds of times myself. As I said:
Sure, I have built a collection of items as a list, because lists are
mutable, then frozen it into a tuple, and *thrown the list away*, then
used the tuple as a key. But that's not the same thing, the intent is
different. In my case, the data was never intended to be a list, it was
always intended to be a fixed record-like collection, the use of list was
as a temporary data structure used for construction. A bit like the idiom
To me, this sounds *exactly* like your use-case: your data, stack traces,
represent a little chunk of immutable data that you build up a line at a
time using a temporary list first, just like I wrote. And I said so.
There's no sign in either your code or your description that the stack
traces get treated as mutable objects in any way once you have finished
building them a line at a time. So your real world, practical, "in the
trenches" example matches my experience: you build a *fixed data record*
using a *temporary list*, throw the list away, and then never mutate that
data record again.
So why are we disagreeing? Like many such discussions on the Internet,
this one has rambled a bit, and I've misunderstood some of your code
(sorry), and you seem to have misunderstood the question I am asking.
Maybe my explanation was not clear enough, in which case, sorry again.
I'm asking about the case where one might want the key to remain mutable
even after it is used as a key, but can't because Python won't let you.
There's no sign that your stack trace example is such an example.
As I earlier said:
But I can't think of any meaningful, non-contrived example where I might
want an actual mutable list of values as a dict key.
and I still can't.
More information about the Python-list