Previous topic

Python Built-in Types and Operations

Next topic

Control Flow Statements

This Page

Assignment operatorΒΆ

Python library reference says:

Assignment statements are used to (re)bind names to values and to modify attributes or items of mutable objects.

In short, it works as follows (simple assignment):

  1. an expression on the right hand side is evaluated, the corresponding object is created/obtained
  2. a name on the left hand side is assigned, or bound, to the r.h.s. object

Things to note:

  • a single object can have several names bound to it:

    >>> a = [1, 2, 3]
    >>> b = a
    >>> a
    [1, 2, 3]
    >>> b
    [1, 2, 3]
    >>> a is b
    True
    >>> b[1] = 'hi!'
    >>> a
    [1, 'hi!', 3]
    
  • By modifying b, we have also modified a! This is because a and b are just labels that point to the same object. If we want to make a copy, we can explicitly ask for one by using the list command:

    >>> a = [1, 2, 3]
    >>> b = list(a)
    >>> b
    [1, 2, 3]
    >>> a is b
    False
    >>> b[1] = 'hi!'
    >>> a
    [1, 2, 3]
    
  • Note that taking slices of lists does perform a copy:

    >>> a = [1, 2, 3]
    >>> b = a[:2]
    >>> b
    [1, 2]
    >>> b[0] = 'hi!'
    >>> b
    ['hi!', 2]
    >>> a
    [1, 2, 3]
    
  • The key concept here is mutable vs. immutable

    • mutable objects can be changed in place.
    • immutable objects cannot be modified once created.

A very good and detailed explanation of the above issues can be found in David M. Beazley’s article Types and Objects in Python.

Copyright: Smithsonian Astrophysical Observatory under terms of CC Attribution 3.0 Creative Commons
 License