Assessment: Mutable vs Immutable Objects in Python

Apr 11, 2013 • Matt Terry

This is in response to [this][1] concept map on mutable vs immutable object in Python.

Novice vs Competent

  1. Name three immutable objects in Python.

  2. Name two mutable objects in Python.

`»> a = ‘african’

b = a
a += ‘ swallow’`

  1. What is a? What is b?

Competent vs Expert

`»> spam = (1,)

eggs = [2,]
breakfast = (spam, eggs)
eggs.append(‘delicious’)
print breakfast`

1) What will the last statement return?
``

`

def tricky_function(keys, values, defaults={'param_1': None}):
    for k, v in zip(keys, values):
        defaults[k] = v
    return defaults

` 2) Explain what is dangerous about this snippet and what mutability's role is?   The competent will know what immutable objects are and how to create them.  The expert will know the interaction between immutable objects and object labels and will be aware of corner cases involving mutable objects.  Identifying the "expert" in mutable/immutable objects is tricky because it can't be done in isolation.  The expert of one concept must have expertise in many concepts first.  It is possible that you cannot become an expert in just one thing at at time.  You must simultaneously become an expert in several things. [1]: http://teaching.software-carpentry.org/2013/03/27/concept-map-mutable-and-immutable-objects-in-python/