[Tutor] Side effect and return value

Albert-Jan Roskam sjeik_appie at hotmail.com
Fri Sep 20 11:02:45 EDT 2024


   On Sep 20, 2024 02:06, dn via Tutor <tutor at python.org> wrote:

     On 20/09/24 08:40, Albert-Jan Roskam wrote:
     >     Hi,
     >     I've just read "Code that fits into your Brain".
     >     The author says a function with a side effect should not have a
     return
     >     value. But how would one write this function in order to do that?

     It took me a moment to realise that what I was (trying to) remembering
     is Mark Seemann's book, "Code that fits in your Head".

   ===
   That's indeed the book I meant. Loved reading it. He also mentioned that
   learning/using functional principles had also been very useful for his OOP
   skills. But I think you're right, one has to be pragmatic sometimes.

     1 Pure Functions: A pure function is one where the output is determined
     solely by its input values, without observable side effects.

     2 Avoiding Side Effects: Functional programming aims to minimize side
     effects, which are changes in state that occur outside a given
     function's scope.

   ====
   Hmmm, the function in my example was not pure in two ways:  the GUIDs in
   the json make the return value impossible to predict AND it has a side
   effect (a record is INSERTed in a table). Oh well. :-)

     @Cameron's post illustrates such very neatly.

   ===
   Did Cameron also reply to my post? I didn't see anything. Is there also a
   news group for Python Tutor? The mailing lists have been so quiet lately.
   Comp.lang.python has more traffic the mirrored mailing list


More information about the Tutor mailing list