Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to judge (or how to write) a python function with no side effects?

The answer equals the definition of side effects.

Up to now, I don't find out a precise answer. The python doc says: Functional style discourages functions with side effects that modify internal state or make other changes that aren’t visible in the function’s return value.

What is modify internal state and make other changes that aren’t visible...?

Is binding varibles to objects(just binding, not modifying) means no side effects? e.g.a=1 or a=[1,2,3] or a,b=1,2.

Here are 4 functions. Are they all with no side effects? why?

Note, assuming the argument n must be an int object.

def purefunc1(n):
    def getn(n):
        return [1,2,3,4,5][:n-1],[1,2,3,4,5][:n]
    def addn(fir,sec,thd):
        return fir+sec+thd
    return addn(getn(n)[0],['HEY'],getn(n)[1])

def purefunc2(n):
    def getn(n):
        #bind
        arr=[1,2,3,4,5]
        return arr[:n-1],arr[:n]
    def addn(fir=[],sec=[],thd=[]):
        return fir+sec+thd
    #bind
    arg1,arg3=getn(n)
    return addn(arg1,['HEY'],arg3)

def purefunc3(n):
    arr=[1,2,3,4,5]
    def getn(n):
        #closure 'arr'
        return arr[:n-1],arr[:n]
    def addn(fir=[],sec=[],thd=[]):
        return fir+sec+thd
    #bind
    arg1,arg3=getn(n)
    return addn(arg1,['HEY'],arg3)

def purefunc4(n):
    def arr():
        return [1,2,3,4,5]
    def getn(n):
        #closure
        return arr()[:n-1],arr()[:n]
    def addn(fir=[],sec=[],thd=[]):
        return fir+sec+thd
    #bind
    arg1,arg3=getn(n)
    return addn(arg1,['HEY'],arg3)

print (purefunc1(3))
print (purefunc2(3))
print (purefunc3(3))
print (purefunc4(3))

My guess:purefunc1 is with no side effects.But I don't know the following purefunc*.

the output is:

[1, 2, 'HEY', 1, 2, 3]
[1, 2, 'HEY', 1, 2, 3]
[1, 2, 'HEY', 1, 2, 3]
[1, 2, 'HEY', 1, 2, 3]    

If you ask why exists such odd functions, the answer is it's just for convenience. The real function is complicated. but if you are interested, you can click here to see whether the function ieval is with no side effects or not.

Thank you all guys in advance.

like image 448
tcpiper Avatar asked Nov 17 '13 04:11

tcpiper


People also ask

How do you stop side effects in Python?

The way to avoid using side effects is to use return values instead. Instead of modifying a global variable inside a function, pass the global variable's value in as a parameter, and set that global variable to be equal to a value returned from the function.

Do Python functions have side effects?

In Python, side effects can occur in two ways: Changing the value of a mutable object. Changing the binding of a variable defined outside the local namespace.

What is a pure function in Python?

A pure function is a function whose output value follows solely from its input values, without any observable side effects. In functional programming, a program consists entirely of evaluation of pure functions. Computation proceeds by nested or composed function calls, without changes to state or mutable data.


1 Answers

In the context of functional programming, a function is a deterministic thing that, given a input, outputs something. That's what pure functions do. They only calculate things. They dont "do" things. If your function code could theoretically be switched by a table (even an infinite one), then you have a function with no side effects.

Anything besides returning a value it's a side effect. The terms "modify internal state" and "make other changes that aren’t visible" refers to that. Modifying internal state or making changes... can means:

  1. Printing something
  2. Modifying a out of scope object
  3. Saving something

You can understand side effect as "modifying the outside world". In your examples, none of them does that. But, if you assigned something to n, like appending something, or anything that would persist in your enviroment after the function returns, then that would be a side effect.

All your functions can be tested comparing inputs and outputs. If you had side effects (like mutating n) your testing would get more complicated, and you would need to analyze the context your function is.

like image 170
Lucas Ribeiro Avatar answered Oct 22 '22 00:10

Lucas Ribeiro