[Tutor] How is "set(ls).add('a') evaluated? [Was: Re: A program that can check if all elements of the list are mutually disjoint]

Alan Gauld alan.gauld at yahoo.co.uk
Sun Jun 6 15:00:39 EDT 2021


On 06/06/2021 18:27, boB Stepp wrote:
> On Sun, Jun 6, 2021 at 6:07 AM Alan Gauld <learn2program at gmail.com> wrote:

> Ah, but even this apparently has at least one exception.  See dn's
> comment about the pop() method where it does return a value (not
> normally None) *and* modifies the object in place.  

Yes, but that's the definition of pop() as an operation.
It takes the top element off the stack and returns it to you.
You couldn't have a pop operation unless it returned the
removed value. It wouldn't make sense. And likewise it
has to modify the stack since otherwise, future pops would
return the same object!


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list