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

Alex Kleider alexkleider at gmail.com
Sun Jun 6 15:42:10 EDT 2021


This may have been mentioned in this thread but if so, I didn't notice it.
Is it not true that functions/methods the names of which are verbs modify
something that exists (and mostly return None.)
Nouns return that to which their name refers/implies.
'pop' does both but as Alan has pointed out, that's implicit in the data
structure.

On Sun, Jun 6, 2021 at 10:28 AM boB Stepp <robertvstepp at gmail.com> wrote:

> On Sun, Jun 6, 2021 at 6:07 AM Alan Gauld <learn2program at gmail.com> wrote:
> >
> >
> > On 06/06/2021 03:32, boB Stepp wrote:
> > > <Gripe>
> > > I find it really hard to remember which functions and methods return
> > > "None" and operate by side effect with those that return a new object.
> >
> > You are not alone. Coming from smalltalk where the default return value
> > of methods is self I find Python's default of none to be quite
> > frustrating at
> > times. As to when it happens the general rule is that if it is a mutable
> > object
> > that is being modified the change happens in-place and None is returned.
>
> 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.  But I take your
> point that this is a very good rule of thumb.  And you have helped me
> feel a little better!  Thanks!
>
> > But every language has its foibles and None returns is one of Python's.
>
> Probably a driver for the creation of new, improved programming languages?
>
> boB Stepp
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list