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

boB Stepp robertvstepp at gmail.com
Sun Jun 6 13:27:11 EDT 2021


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


More information about the Tutor mailing list