Re: pop method question



On Sat, 03 Mar 2007 15:56:39 -0500, Nicholas Parsons wrote:


On Mar 3, 2007, at 3:49 PM, Paul Rubin wrote:

Nicholas Parsons <parsons.nicholas1@xxxxxxxxx> writes:
I was just playing around in IDLE at the interactive prompt and typed
in dir({}) for the fun of it. I was quite surprised to see a pop
method defined there. I mean is that a misnomer or what? From the
literature, pop is supposed to be an operation defined for a stack
data structure. A stack is defined to be an "ordered" list data
structure. Dictionaries in Python have no order but are sequences.
Now, does anyone know why the python core has this pop method
implemented for a dictionary type?

Try typing:

help({}.pop)
--
http://mail.python.org/mailman/listinfo/python-list

Thanks, that gives a more details explanation of what the behavior is
but doesn't answer my question above :(

Just because pop can be defined for an ordered stack doesn't mean pop
can't be generalized to other data types too.

I personally don't see that pop has any advantage, especially since the
most useful example

while some_dict:
do_something_with(some_dict.pop())

doesn't work. Instead you have to write this:

for key in some_dict.keys():
# can't iterate over the dictionary directly!
do_something_with(some_dict.pop(key))

which is hardly any saving over:

for key in some_dict.keys():
# can't iterate over the dictionary directly!
do_something_with(some_dict[key])
del some_dict[key]


To my mind, having to supply a key to dict.pop makes it rather pointless.


--
Steven.

.



Relevant Pages

  • Re: pop method question
    ... data structure. ... A stack is defined to be an "ordered" list data ... does anyone know why the python core has this pop method ... implemented for a dictionary type? ...
    (comp.lang.python)
  • Re: pop method question
    ... data structure. ... A stack is defined to be an "ordered" list data ... does anyone know why the python core has this pop method ... implemented for a dictionary type? ...
    (comp.lang.python)
  • Re: foreach and stack, iterating from the bottom-up?
    ... precise) and I was a bit surprised that it seems to iterate from the ... bottom up. ... Stack extends Vector, Vector implements List, List.iterator has very ... Also, generally you don't "Iterate" stacks, you push onto and pop from ...
    (comp.lang.java.programmer)
  • Re: foreach and stack, iterating from the bottom-up?
    ... precise) and I was a bit surprised that it seems to iterate from the ... bottom up. ... Most likely an unwelcome consequence of implementing Stack ... Stack would be at the high-index end of the Vector (to avoid ...
    (comp.lang.java.programmer)
  • Re: How to get stack from every thread when doing crash dump?
    ... On Jun 23, 2005, at 5:39 PM, Howard wrote: ... >> You can iterate over all the threads. ... >> and for each thread dump the stack, ...
    (freebsd-arch)