pop method question

Nicholas Parsons parsons.nicholas1 at gmail.com
Sun Mar 4 07:36:50 EST 2007


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




On Mar 3, 2007, at 10:40 PM, MonkeeSage wrote:

> Nick,
>
> In regards to stack-like objects, pop() implies mutation of the
> reciever and returning the item 'popped' off the stack. The same
> _semantic_ meaning can be used for pop() regarding dictionaries, even
> though the _implementation_ would be different: dict.pop(key) mutates
> the reciever and returns the value associated with the key.
>
> Regards,
> Jordan
>
> -- 
> http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list