Page 245 -
P. 245

custom data objects







                                                            Do this!





                                          In your code, replace your Athlete
                                          class code with your new AthleteList
                                          class code, and don’t forget to change
                                          get_coach_data() to return an
                                          AthleteList object instance as
                                          opposed to an Athlete object instance.










           Q:   Sorry…but not three minutes ago you were telling me not   Q:  Can I inherit from my own custom classes?


           to expose the inner workings of my class to its users, because
           that was fundamentally a bad idea. Now you’re doing the exact   A: Of course, that’s the whole idea. You create a generic class
           opposite! What gives?                            that can then be “subclassed” to provide more specific, targeted
                                                            functionality.
          A: Well spotted. In this particular case, it’s OK to expose the fact   Q:
           that the class is built on top of list. This is due to the fact that the    Can I put my class in a module file?
           class is deliberately called AthleteList to distinguish it from
           the more generic Athlete class. When programmers see the   A: Yes, that’s a really good idea, because it lets you share your
           word “list” in a class name, they are likely to expect the class to work   class with many of your own programs and with other programmers.
           like a list and then some. This is the case with AthleteList.  For instance, if you save your AthleteList class to a file
           Q:   And I can inherit from any of the built-in types?  called athletelist.py, you can import the into your code
                                                            using this line of code:

          A: Yes.                                                from athletelist import AthleteList
           Q:   What about inheriting from more than one class…does
                                                            then use the class as if it was defined in your current program. And,
           Python support multiple interitance?             of course, if you create a really useful class, pop it into its own
                                                            module and upload it to PyPI for the whole world to share.
          A: Yes, but it’s kind of scary. Refer to a good Python reference text
           for all the gory details.





                                                                                      you are here 4    209
   240   241   242   243   244   245   246   247   248   249   250