-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?

under_the_sea_salad
  • 1,754
  • 3
  • 22
  • 42
  • Immutable objects are faster. – TigerhawkT3 Oct 14 '15 at 22:58
  • 4
    What could you possibly change about the number `1`? – Colonel Thirty Two Oct 14 '15 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. – under_the_sea_salad Oct 14 '15 at 23:00
  • @ColonelThirtyTwo number `1` doesn't have to change, but I would like to be able to change the variable that points to `1` to instead point to `2` without having to create a new variable that points to `2` and then returning that variable and then overriding the original object that pointed to `1`. – under_the_sea_salad Oct 14 '15 at 23:02
  • Yes, there are paradigm advantages. That's why Python also provides mutable objects. Choose what you need. If you have some particular paradigm that requires all mutable objects, then switch from Python to another language. Is this a problem for you? – Prune Oct 14 '15 at 23:03
  • 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. – Colonel Thirty Two Oct 14 '15 at 23:03
  • While [THIS](http://stackoverflow.com/a/3770485/298607) is a Java answer, the concept is the same. – dawg Oct 14 '15 at 23:04
  • Your basic problem in the example you posted is use of variables within their contexts, not the mutability of objects. – Prune Oct 14 '15 at 23:04
  • 1
    @bourbaki4481472 - perhaps you should've actually tested the code you posted. – TigerhawkT3 Oct 14 '15 at 23:06

1 Answers1

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]
Josh J
  • 6,813
  • 3
  • 25
  • 47
  • Oh, that is actually a much clearer example of what I meant to say! – under_the_sea_salad Oct 14 '15 at 23:09
  • I think this is probably wrong. New variables aren't being created, the same parameter variables are simply being reassigned. Here's a demonstration: http://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). – Asad Saeeduddin Oct 14 '15 at 23:17
  • The answer is right. The question and example were wrong. – Josh J Oct 15 '15 at 03:17