Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Side Effects in Python

Tags:

python

I read this in the python docs: Docs

requiring global for assigned variables provides a bar against unintended side-effects.

Let's put this in code:

def double(n):
  global y 
  y = 2 * n # this turns global as we have state y explicitly global

y = 5 
double(y)
print y # outputs 10

I would like to double check my understanding if the above code has a side effect at global y if so I feel this is contradictory to the statement in the docs, basically I think requiring global for assigned variables does not guard against side effects.

Please correct me if I'm wrong.

Thanks

like image 614
InquisitiveGirl Avatar asked May 18 '17 00:05

InquisitiveGirl


2 Answers

Requiring global for assigned variables provides a bar against unintended side-effects.

When the docs say that global provides a bar against unintended side-effects, it means that it safeguards you against situations where a function could change its outer scope because of name aliasing.

Here is an example:

x = 5

def safedouble(x)
    x = 2 * x
    return x

def unsafedouble(x)
    global x
    x = 2 * x
    return x

print(x) # 5
print(safedouble(x)) # 10
print(x) # 5 The x inside of the function does not interfere with x outside =)
print(unsafedouble(x)) # 10
print(x) # 10 ... do we really want this to happen?!

Requiring global means that functions only mutate the outer scope if the programmer explicitly asks for it.

like image 92
dangom Avatar answered Oct 19 '22 00:10

dangom


No, you misunderstand. Here is what the docs state in context:

In Python, variables that are only referenced inside a function are implicitly global. If a variable is assigned a value anywhere within the function’s body, it’s assumed to be a local unless explicitly declared as global.

Though a bit surprising at first, a moment’s consideration explains this. On one hand, requiring global for assigned variables provides a bar against unintended side-effects. On the other hand, if global was required for all global references, you’d be using global all the time. You’d have to declare as global every reference to a built-in function or to a component of an imported module. This clutter would defeat the usefulness of the global declaration for identifying side-effects.

Assignment to a name does not require global. You can use assignment without global, but the docs are telling you that if you do use assignment in a local scope without the global directive, the name will be considered a local variable by default! The reasoning is as stated, if assigned-to variables inside a function could be global by default, then you might accidentally assign to a global variable and inadvertently cause a side-effect. But if you require a global directive, then you will know that your function produces a side-effect. It isn't stating that global prevents side-effects, it prevents inadvertent side-effects.

like image 42
juanpa.arrivillaga Avatar answered Oct 18 '22 22:10

juanpa.arrivillaga