0

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?

2

3 Answers 3

2

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.
1

"Operates by side effect" here means that the method modifies the list that you pass in by reversing it in-place. This might not be the behavior you'd expect - you might expect that the list wouldn't get modified, but that the method would return a reversed copy (this is what reverse(list) does). Another option would be that the list gets reversed in place, but that the method would return the list, so you could chain operations like list.reverse().sort(). The documentation is explaining why this doesn't happen: it returns None so that if you mistakenly thought that this method returned a copy without modifying the original list, you'd quickly be proven wrong.

It's a little confusing that the operation-in-place is called a "side effect", when in this case it's the whole point of the method; but "side effect" is a general term for operations that mutate an object, rather than (or in addition to) producing a new object.

1
  • That meaning of the term in the last line, is exactly the thing I was expecting to know. Jul 12, 2017 at 7:26
1

The term "side effect" is jargon from functional programming, a style of programming that revolves entirely around functions. Python has functions, but there are other languages that do a lot more with them than Python does.

In functional programming, a function that does not use any information from outside itself other than its parameters, and that also does not affect anything outside itself but only returns a value, is referred to as a pure function. A characteristic of a pure function is that you always get the same result if you call it with the same parameters. Functional peeps say using pure functions makes it a lot easier to reason about code, because you don't have to be aware of the entire program state.

If a function affects something outside itself, that's called a side effect. The function takes parameters and returns a value, because that's what a function does, but it also does one or more other things. Changing the contents of a mutable argument is a side effect. (Pure functional languages—languages that only allow pure functions—don't have mutable containers, making this impossible. Instead, they construct new objects and return those. It's as if Python had tuples and no lists.)

Printing something to standard output, or writing a file, or any other form of output, is also a side effect. So you may wonder: how do pure functional languages do output, if side effects are not allowed? The answer is, very carefully.

Python has many functional programming constructs. If they intrigue you, investigate learning Scheme or Haskell or F#, or some other functional language.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.