pop method question

Nicholas Parsons parsons.nicholas1 at gmail.com
Sat Mar 3 21:55:39 EST 2007


Hi Raymond,

Thank you for your clarification below.  I was just using "remove"  
and "delete" as possible alternatives to the name "pop" without much  
contemplation.  Like you say below, it begs the question as to why  
not have two separate operations for dictionaries (retrieval of value  
from key followed by deletion of key) instead of one method.

I'm not sure I agree with you said about the pedantic usage of pop.   
I would rather have a term mean one thing instead of several causing  
ambiguity.  Just from my computer science background when I see pop 
(), I think of a stack data structure.  Why muddle the waters by  
introducing another meaning for the term?  There are plenty of other  
words to use that could describe the behavior exhibited by the  
dictionary operation of removing a key and returning its value.  The  
notion of a stack and pop() and push() methods for it are very  
important from a historical perspective and is not just some fad.

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?  Of course Java does cheat  
a little by overloading the '+' operator for string objects but that  
is a built-in feature of language.  Also some people find '+' more  
appealing to the eye than a method call like add() in their code.

Even in the case of C, we have some ambiguity with the dangling if  
statement.  So I guess you can't win for trying :).

Just call me a purist and think of me as someone who likes  
consistency.  Python is here to stay and no language is perfect...

--Nick

On Mar 3, 2007, at 7:32 PM, Raymond Hettinger wrote:

> [Nicholas Parsons]
>>  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?
>>
>> I realize that in this context it is used for removing a specific key
>> from the current dictionary object.  But why call it pop and not
>> something more intuitive like remove or delete?
>
> The naming for pop() method followed from the naming of the previously
> existing popitem() method.  Neither "remove" nor "delete" would have
> been a good name for a method that looked-up and returned a value as
> well as mutating the dictionary.  The word "pop" on the other hand
> strongly suggests both retrieval and mutation.
>
> The notion that "pop" is only defined for stack operations is somewhat
> pedantic.  We have also successfully used the name for sets, dicts,
> and deques (success meaning that most people just "get it" and are
> able to learn, use, read the name without difficultly).
>
> The rationale for the pop() method was that the pattern "v=d[k]; del
> d[k]" could be collapsed to a single call, eliminating a two
> successive look-ups of the same key.  Looking back, this rationale is
> questionable because 1) the second lookup is not expensive because the
> first lookup put the relevant hash entries in the cache, and 2) the
> lookup/delete pattern for dictionaries does not seem to arise often in
> practice (it does come-up every now and then in the context of set
> operations).
>
> There was also a notion that threaded programming would benefit by
> having lookup-then-delete as an atomic transaction.  It is unknown to
> me whether that purported benefit has ever been realized.
>
>
> Raymond Hettinger
>
> -- 
> http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list