-3

So Python is pass by reference. Always. But objects like integers, strings, and tuples, even when passed into a function, cannot be changed (hence they are called immutable). Here is an example.

def foo(i, l):
  i = 5 # this creates a new variable which is also called i
  l = [1, 2] # this changes the existing variable called l

i = 10
l = [1, 2, 3]
print(i)
# i = 10
print(l)
# l = [1, 2, 3]
foo(i, l)
print(i)
# i = 10 # variable i defined outside of function foo didn't change
print(l)
# l = [1, 2] # l is defined outside of function foo did change

So you can see that the integer object is immutable while the list object is mutable.

What's the reason for even having immutable objects in Python? What advantages and disadvantages would there be for a language like Python if all the objects were mutable?

9
  • Immutable objects are faster. Commented Oct 14, 2015 at 22:58
  • 4
    What could you possibly change about the number 1? Commented Oct 14, 2015 at 22:59
  • @TigerhawkT3 I can see how that is true, but certainly there could be paradigm advantages to a language even if some commonly used objects were slow. Commented Oct 14, 2015 at 23:00
  • 1
    You're confusing immutability with scoping. The i variable in foo is not the same as the global i variable. You can add global i above the i = 5 in foo, but using globals is bad practice because it makes code harder to reason about. Commented Oct 14, 2015 at 23:03
  • 1
    @bourbaki4481472 - perhaps you should've actually tested the code you posted. Commented Oct 14, 2015 at 23:06

1 Answer 1

0

Your example is incorrect. l did not change outside the scope of foo(). i and l inside of foo() are new names that point to new objects.

Python 2.7.10 (default, Aug 22 2015, 20:33:39)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> def foo(i, l):
...   i = 5 # this creates a local name i that points to 5
...   l = [1, 2] # this creates a local name l that points to [1, 2]
...
>>> i = 10
>>> l = [1, 2, 3]
>>> print(i)
10
>>> print(l)
[1, 2, 3]
>>> foo(i, l)
>>> print(i)
10
>>> print(l)
[1, 2, 3]

Now if you changed foo() to mutate l, that is a different story

>>> def foo(i, l):
...     l.append(10)
...
>>> foo(i, l)
>>> print(l)
[1, 2, 3, 10]

Python 3 example is the same

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 23 2015, 02:52:03)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> def foo(i, l):
...   i = 5 # this creates a new variable which is also called i
...   l = [1, 2] # this changes the existing variable called l
...
>>> i = 10
>>> l = [1, 2, 3]
>>> print(i)
10
>>> print(l)
[1, 2, 3]
>>> foo(i, l)
>>> print(i)
10
>>> print(l)
[1, 2, 3]
Sign up to request clarification or add additional context in comments.

3 Comments

Oh, that is actually a much clearer example of what I meant to say!
I think this is probably wrong. New variables aren't being created, the same parameter variables are simply being reassigned. Here's a demonstration: ideone.com/LJt4AY. That said, I don't see how this is an answer to the question (i.e. why are there immutable objects in Python).
The answer is right. The question and example were wrong.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.