0% found this document useful (0 votes)
10 views2 pages

Side Effects

The document discusses the concept of side effects in programming, specifically in the context of Python functions. It explains how side effects differ from return values, using the example of a function that prints to the terminal and modifies a global variable representing a machine's emotional state. The speaker emphasizes the importance of understanding side effects for good programming practices, as they can complicate debugging and code maintenance.

Uploaded by

a.bruce.ed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Side Effects

The document discusses the concept of side effects in programming, specifically in the context of Python functions. It explains how side effects differ from return values, using the example of a function that prints to the terminal and modifies a global variable representing a machine's emotional state. The speaker emphasizes the importance of understanding side effects for good programming practices, as they can complicate debugging and code maintenance.

Uploaded by

a.bruce.ed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

SPEAKER: Well, hello, one, and all, and welcome to our short on side effects.

So
we've seen that functions are these things in programming languages that take
inputs and produce outputs for us. Now, we've seen really that there are various
kinds of outputs. One of them is called a return value, the value the function
gives back to us after it finishes running. But it turns out there's also a kind of
output known as a side effect, which is not the return value but anything else that
gets changed while our function is running.

So let's take a peek at an example here. Here I have a program called machine.py
representing some machine with some internal emotional state. Notice how, up at the
top here, I have an emoticon representing our machine's emotions at this given
point in time. In this case, it has this particular emoticon representing something
like sadness or forlornness, and so we'll see if we can make our machine more happy
as you write our program.

But ideally, we want this machine to say something to us, and so let's go ahead and
write ourselves a function called say that we can then call inside of our main
function here. So I will, underneath main, go ahead and write for myself a function
called say, using the Python keyword def and the name for the function, say. And
maybe say should take as input as an argument here maybe something called phrase,
the phrase to say. And so by convention, I will indent within this code block here
to write the body of my function, the code I want to run when say is called or used
in my program.

Well, what should say do? I think, really, to say something, our machine should
print something to the terminal. And so we can do that with a Python function known
as print, just like this. And what should print print? Well, probably the phrase in
this case, the phrase we gave as input to say.

Now, if we're just using print, we haven't really added much functionality here to
the say function, so why don't we have the machine able to say something but then
maybe append the emoticon at the end so it kind of says something with emotion
here? I'll go ahead and say print the phrase but then also print on a space
followed by the emoticon itself up top here.

So notice how phrase is really a local variable. It's passed as input to this
function say and used inside that function. Emoticon, though, as a global variable
defined at the top of our file here, is able to be accessed by any particular
function-- in this case, say included. So ideally, say should then say whatever
phrase we have followed by a space followed by the emoticon representing our
machine's emotions.

So why don't we try this out in the main function, which will be run as we run our
program here? I'll click. I'll say say. Maybe, "Is anyone there?" And so if we run
our machine down below-- I'll say python machine.py. Hit Enter, and we'll see, "Is
anyone there?" with a little bit of a sad face. So we made ourselves a sad machine,
but let's go ahead and try to make it happier as we go through. So maybe it could
later on say something like "Oh, hi!" just like that. And if I were to go ahead and
run this, I'll say python machine.py. Hit Enter, and we see now two lines of text.
"Is anyone there?" and "Oh, hi!"

Now, there are some things to fix. Ideally the emoticon would be happy in the
terminal down below when it says "Oh, hi!" But before we go there, let's first take
stock of what we've done. We've actually written a program that has a side effect.
This is one example of a side effect, printing to the terminal in this case.

So our function say has the side effect of printing to the terminal, but what other
kinds of side effects might there be? Well, we mentioned earlier that a side effect
is really anything that gets changed while our function is running that is not the
return value we actually give back at the end of our function. So maybe inside of
main, we could try to change the emotional state of our machine as a side effect.

So let's try this out here. I'll clear my terminal with Control-L, and then I'll go
on to line six here. And before we say "Oh, hi!", let's try maybe making the
emotion a little happier in this case, I'll say colon D for the big smiley face.
And, ideally, if we were to run this program, we'd see, "Is anyone there?" with a
bit of a sad face followed by "Oh, hi!" with a bit of a happy face.

I'll run python machine.py, and we seem to still have ourselves a sad machine. So
how can we improve this? Well, notice first here that emoticon, if you can see very
slightly, is grayed out. And this is because Python, by default, wants us to
specify that we're trying to change some global variable inside of a function, this
more local scope. So our side effect we want to have happen is to change this
global variable, but because this isn't always desirable, Python wants us to
explicitly specify what we're going to do.

So to do this within the main function, what I can do is use this keyword called
global, and I can specify after global the global variable I want to make
accessible and modifiable within this particular function. And key thing there is
it's now modifiable, not just accessible.

Notice how in say it was accessible to us, emoticon was, but in main, it wasn't
modifiable. I couldn't update this variable. If I now say global emoticon, I'm able
now to modify the actual value of that variable as a side effect-- not the return
value here but just a side effect as this function runs.

So here again, we have our global emotional state for our machine. We then have our
function called main. Within that function, we say we're going to be able to change
this global emotional state that we called emoticon. We'll first say, "Is anyone
there?" change the emotion, and then say "Oh, hi!"

So let's try this out. I'll go ahead and run python machine.py, and there we go.
We've now seen another version of a side effect, one that changes some global
variable.

Now, in programming, this may or may not be desirable. If you have a global
variable that is accessed across many other functions, it can be hard to debug your
code if that variable gets modified across all of these different functions. So
keep in mind when you use side effects like these, make sure it's a good design
decision to actually make when you're writing your program.

But here we've seen two examples, printing to terminal, modifying some global
state. There are others too, but I'll leave that for a later time. We'll see you
next time.

You might also like