Home > Articles > Open Source > Python

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

4.9 Critique

Now let's criticize the design of these set classes. On the positive side, they do make good use of object-oriented programming techniques, and they do allow more than one implementation of sets to be used interchangeably in the same program.

On the negative side, there are two points:

First, they are not complete. There ought to be a relative complement method to give a new set containing all the elements of one set that are not in another. Although it could be programmed, it's used a lot and it's logically one of the standard set operations.

Second, they do not have identical interfaces. You can put lists into other lists and search for them, but you cannot make list keys for hash tables, so there are operations that will succeed for ListSets that will fail for DictSets. In the following code, we create a ListSet and a DictSet and try to insert a list, [7,8], into each and look it up. We succeed only for the ListSet.

>>> import DictSet
>>> import ListSet
>>> y=ListSet.ListSet([4,5,6])
>>> x=DictSet.DictSet([1,2,3])
>>> x
{3, 2, 1}
>>> y
{4, 5, 6}
>>> y.insert([7,8])
{4, 5, 6, [7, 8]}
>>> y.contains([7,8])
1
>>> x.insert([7,8])
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "DictSet.py", line 14, in insert
    self.rep[x]=x
TypeError: unhashable type
  • + Share This
  • 🔖 Save To Your Account