Page 242 -
P. 242

idle session







              Let’s see what’s involved in inheriting from Python’s built-in list class. Working in IDLE’s shell, start by creating a
              custom list derived from the built-in list class that also has an attribute called name:
              >>> class NamedList(list):            Provide the name of the class that this new
                     def __init__(self, a_name):    class derives from.
                             list.__init__([])
                             self.name = a_name   Initialize the derived from class, and then
                                                  assign the argument to the attribute.

              With your NamedList class defined, use it to create an object instance, check the object’s type (using the
              type() BIF), and see what it provides (using the dir() BIF):

              >>> johnny = NamedList("John Paul Jones")      Create a new “NamedList” object instance.
              >>> type(johnny)
                                              Yes, “johnny” is a “NamedList”.
              <class '__main__.NamedList'>
              >>> dir(johnny)
              ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dict__', '__doc__',
              '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__',
              '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__module__',
              '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__',
              '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__',
              '__weakref__', 'append', 'count', 'extend', 'index', 'insert', 'name', 'pop', 'remove',
              'reverse', 'sort']
                            “johnny” can do everything a list can, as well as store data in the “name” attribute.
              Use some of the functionality supplied by the list class to add to the data stored in johnny:
              >>> johnny.append("Bass Player")                         Add data to the “NamedList” using the
              >>> johnny.extend(['Composer', "Arranger", "Musician"])   methods provided by the list built in.
              >>> johnny
              ['Bass Player', 'Composer', 'Arranger', 'Musician']
              >>> johnny.name
                                            Access the list data, as well as
              'John Paul Jones'             the attribute data.

              Because johnny is a list, it’s quite OK to do list-type things to it:
              >>> for attr in johnny:                             “johnny” is like any other list, so feel free to
                      print(johnny.name + " is a " + attr + ".")   use it wherever you’d use a list.

              John Paul Jones is a Bass Player.
              John Paul Jones is a Composer.
              John Paul Jones is a Arranger.
              John Paul Jones is a Musician.

                                 Confirmation: John’s a busy boy. §
           206    Chapter 6
   237   238   239   240   241   242   243   244   245   246   247