Apurva Kunkulol Apurva Kunkulol - 3 years ago 84
Python Question

Operate by side effect

From the documentation of Python :
Comment about the list.reverse() method

It says that the method doesn't return a new list to remind you that it operates by side effect. So what does this "operate by side effect" mean?

Answer Source

The primary effect of a method is that it returns the affected value. Some method work in place in that they modify the object without returning the modified version.

This is sometimes called a "side effect" because the method does not return the updated object.

Like .reverse(), .sort() is another method that works in-place.

The key thing to remember is that these functions do not return any value - their implicit return value is None and to avoid assigning the return value.

Here is an example on what not to do:

>>> i = [3, 4, 0]
>>> reversed_i = i.reverse()
>>> reversed_i # reversed_i is None, the only value not printed by the Python shell.
>>> i 
[0, 4, 3] # The original object is modified.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download