pop method question

  • Thread starter Nicholas Parsons
  • Start date
H

Hendrik van Rooyen

Alex Martelli said:
Worse: it's totally wrong. It's also defined for eyes, as a musical
genre, as a kind of soda, as an avant-garde artistic movement of the
'50s, for baloons, as a parent of the male persuasion, for email
reading, and moreover it's often used to refer to Persistent Organic
Pollutants or Points Of Presence -- not forgetting weasels, either.
One should never forget the pawning of weasels.

Then there is the contrary meaning - instead of "take out" it can
mean "put in" - Just pop it in the oven, there's a dear...

- Hendrik
 
M

Marc 'BlackJack' Rintsch

Just from my computer science background when I see pop(), I think of a
stack data structure.

Then question your presumptions. There are also many people thinking
`list` must be something with nodes and pointers when they see the
interface and usage of Python lists.
But then again, there are other examples of ambiguity in the python
language such as allowing operators like '+' to be overloaded. Why not
just have a "add()" method like Java?

Why does this remove ambiguity? I even would expect different behaviour
from both. I expect the ``+`` operator to return either an immutable or
entirely new object, while `add()` can be something on containers that
mutates the object.

Ciao,
Marc 'BlackJack' Rintsch
 
N

Nicholas Parsons

Hi Jordan,

That is true what you say about pop() behavior with stack-like
objects. But the definition of pop() for a stack-like structure is
stronger than that. A stack is a LIFO data structure. Therefore the
pop() operation is defined to not only mutate the receiver and return
the item popped but also ensure that the LAST item in the stack is
removed. This makes perfect sense for a list type in python since
lists are mutable sequences that have a specified order. But for
dictionaries this does not hold since they are unordered sequences by
definition. So for dictionaries it would not make sense for a
programmer to simulate a stack-like type.

While we're on the subject I'm also curious as to why the author of
the built-in list class called the method "append" to add an element
to the end of a list and not something like "push". But this is not
really much of a problem if the programmer could create an alias for
it. Is it possible to do this in python? I know you can do it in
ruby and even override methods and attributes in an existing built-in
class like String for instance.

--Nick
 
N

Nicholas Parsons

Then question your presumptions. There are also many people thinking
`list` must be something with nodes and pointers when they see the
interface and usage of Python lists.

Good point :).
Why does this remove ambiguity? I even would expect different
behaviour
from both. I expect the ``+`` operator to return either an
immutable or
entirely new object, while `add()` can be something on containers that
mutates the object.


This is true. As long as the language (be it Java, Python, X) itself
remains consistent over time, then this should be fine.
 
S

Steven D'Aprano

Hi Jordan,

That is true what you say about pop() behavior with stack-like
objects. But the definition of pop() for a stack-like structure is
stronger than that.

That's okay, we're not talking about pop for stack-like structures, we're
talking about pop for dictionaries.

A stack is a LIFO data structure. Therefore the
pop() operation is defined to not only mutate the receiver and return
the item popped but also ensure that the LAST item in the stack is
removed.

Not so. Pop can also be defined to take an argument telling the data
structure (not necessarily a stack) which item to remove.

Pop can also operate on queues, in which case it removes the FIRST item in
the queue.

Pop can also be defined for dequeues (double-ended queue), in which case
it can remove from either the first or the last position.

Pop can also be defined for balloons, which is what they do when you stick
a pin in them.


This makes perfect sense for a list type in python since
lists are mutable sequences that have a specified order. But for
dictionaries this does not hold since they are unordered sequences by
definition.

That's okay, since unordered sequences can define pop too. Obviously pop
for a dictionary has to be defined slightly differently, but the essential
characteristics -- remove an item and return it -- remain the same.

So for dictionaries it would not make sense for a
programmer to simulate a stack-like type.

Dictionaries don't simulate stacks.

We can talk about pushing a value onto a stack, and about pushing a
value into a register, and yet registers are not stacks. We use the
same word, multiply, for both scalar multiplication (3*5=15), matrix
multiplication, and mixed scalar-to-matrix multiplication, even though
they are quite different. (For starters, XY = YX if X and Y are both
scalars, but not if they are matrices.)

We use "concatenate" to describe the different procedures of joining
linked lists, arrays and strings. We talk about "inserting" into linked
lists, B-trees, heaps, arrays and priority queues.

Why shouldn't we talk about popping a value from a dictionary?

While we're on the subject I'm also curious as to why the author of
the built-in list class called the method "append" to add an element
to the end of a list and not something like "push".

Well, gosh, I really don't know what they were thinking, using "append" as
the name of the method that, um, APPENDS an item to the end of a list. How
confusing is that?

But this is not
really much of a problem if the programmer could create an alias for
it.

It isn't a problem at all.
 
G

Gabriel Genellina

En Sat, 03 Mar 2007 19:55:16 -0300, Steven D'Aprano
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 such constructs, one can use popitem:

while some_dict:
do_something_with(some_dict.popitem()[1])

The popitem method is older than pop. I don't like pop either.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top