Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do list operations in python operate outside of the function scope? [duplicate]

Tags:

python

scope

list

In the python code below, variable number is passed to the function addone, and a local copy is operated on. The value of number stays the same.

def addone(num):
    num = num + 1
    print "function: added 1, now %d" % num

number = 5
print "Before:", number
addone(number)
print "After:", number

Output :

Before: 5
function: added 1, now 6
After: 5

However, the behavior appears to be different with list operations like pop, append etc. This somewhat confuses me. Do all list operations operate globally ? If so, is there any particular reason behind it ?

def pop_first(stuff):
    popped = stuff.pop(0)
    print "function: '%s' was popped!" % popped

words = ["A", "list", "of", "words"]
print "Before:", words
pop_first(words)
print "After:", words

Output :

Before: ['A', 'list', 'of', 'words']
function: 'A' was popped!
After: ['list', 'of', 'words']
like image 661
peonicles Avatar asked Apr 12 '14 11:04

peonicles


2 Answers

The short answer is because lists are mutable and integers are immutable.


You cannot mutate an integer in place, so we call it 'immutable'. With this in mind, things like addition on an integer do not modify the original object, but rather return a new value- so your original variable will remain the same. Therefore, if we store a reference to an integer, they will only be the same object as long as we have not changed either one of them:

>>> foo = 1
>>> bar = foo
>>> foo is bar
True
>>> foo += 2
3
>>> foo
3
>>> bar
1
>>> foo is bar
False

On the other hand lists are 'mutable' (can modify that same object reference), and operations like pop() mutate the list in-place, changing the original. This also means that if you edit a reference to a mutable object such as a list, the original will be changed as well:

>>> baz = [1, 2, 3, 4, 5]
>>> qux = baz
>>> qux is baz
True
>>> baz.pop()
5
>>> qux
[1, 2, 3, 4]
>>> baz
[1, 2, 3, 4]
>>> qux is baz
True
like image 155
anon582847382 Avatar answered Oct 20 '22 00:10

anon582847382


Passing objects to functions works the same way as assigning them. So, what you're seeing is the same effect as this:

>>> words = ["A", "list", "of", "words"]
>>> stuff = words
>>> stuff.pop()
'words'
>>> words
['A', 'list', 'of']

This happens because stuff and words are the same list, and pop changes that list. ints are immutable, meaning they don't support any in-place mutations: every time you change its value, it gives you a different int object with the new value. You can test whether two objects are the same or distinct objects using the is operator:

>>> stuff is words
True
>>> a = 5
>>> b = a
>>> a is b
True
>>> b += 1
>>> a is b
False
like image 1
lvc Avatar answered Oct 19 '22 23:10

lvc