educative.io

Altering immutable variables using function

Hi,
I was going though the
Function Scope

of python and saw the last part showing us how functions can alter a python object, in that case a list object.

Can you help me out with one confusion, can a function alter any mutable data types/objects or only lists?

thanks in advance

Hi Raiyeem. A function can alter any mutable data type objects. These include:

  • lists
  • dicts
  • sets

Functions take in the reference of a mutable object. So any changes made inside are reflected outside. However, there is one thing to note. We cannot assign a new mutable object to a variable inside a function. The original object (the original list) can be mutated and edited, but if the new list is assigned to the variable, this list will not be reflected in the outside scope.

Go ahead and try that out in the last coding window of the lesson. Just some extra information to have :slight_smile: