Homework Solution

Deeper Understanding

Problems to be Submitted (18 points)

  1. (6 points)

    Python's list.__contains__ method has semantics that check for whether any element of the list is equivalent to the given parameter. This can be demonstrated with the following experiment:

    >>> x = range(5)
    >>> y = range(5)
    >>> print id(x)
    3695160
    >>> print id(y)
    3689488
    >>> x == y
    True
    >>> x is y		
    False
    >>> sample = [ x ]
    >>> print y in sample
    True
    
    Notice that the sample list has x as an element, yet it reports the distinct list y to be in the list, because y is equivalent to x. Had the semantics been looking for the precise identity, the last containment query would have returned False.

    A simpler example can be constructed noting that floats and ints are distinct but can be equivalent, that is 4 == 4.0 is true yet 4 is 4.0 is false. We can then observe

    >>> data = [4]
    >>> 4.0 in data
    True
    
    and conclude that the containment check for lists suffices if it locates an equivalent element.

  2. (6 points)

  3. (6 points)

    1. This is a boolean test to see whether I live in the same actual house as you (that is, if we are housemates).

    2. This is a boolean test to see whether you and I live in houses that are considered to be "equivalent" to each other (even though they may actually be two distinct houses). The notion of equivalence would have to be defined by the House class, perhaps capturing houses that have a similar blueprint or similar market values.

    3. This assignment statement would be used if I were moving into your house. Notice that the identity of yourHouse is unchanged by this assignment. It is myHouse that has changed.

    4. This creates a new house for my use. That house is a "shallow" copy of your house. There is not a great real-world analogy of such a shallow copy, but perhaps it could be something like these two house instances still sharing something like a phone line, utility bill, or some other such indirect attribute of the house.

    5. This creates a new house for my use, as a "deep" copy of your house. My house would be its own house, modeled precisely upon the design of your house, including furnishings that are copies of your own, right down to the food inside the refrigerator inside the kitchen, and new copies of any indirect references such as phone lines and utilities.

    6. yourHouse = myHouse
      This (re)assigns the notion of yourHouse to be a reference to the existing house that I am using. After the assignment, we will both be living in what was originally my house.


Extra Credit

  1. (2 points)

    The problem stems from the interpretation of the condition

            if not index:                 # nothing sent by caller
    
    When index is None, this properly triggers the default behavior. However, when index is an integer, this expression is evaluated such that the value 0 is treated as False and all nonzero values are True. Unfortunately, this means that an explicit call of pop(0) inadvertently deletes the last element of the list (rather than the first).


Last modified: Wednesday, 14 April 2010