10.1 Introduction

  • Section 1.2 introduced the basic terminology and concepts of object-oriented programming
  • Everything in Python is an object

Crafting Valuable Classes

  • Create your own custom classes
  • Focus on “crafting valuable classes” that help you meet application requirements
  • Use object-oriented programming
    • classes, objects, inheritance and polymorphism
  • Object-oriented programming makes it easier to design, implement, test, debug and update applications

Class Libraries and Object-Based Programming

  • Vast majority of object-oriented programming in Python is object-based programming
    • Create and use objects of existing classes
  • To take maximum advantage of Python you must familiarize yourself with lots of preexisting classes
  • Easy for you to reuse existing classes rather than “reinventing the wheel”
  • Widely used open-source library classes are more likely to be thoroughly tested, performance tuned and portable across a wide range of devices, operating systems and Python versions
  • Find abundant Python libraries on sites like GitHub, BitBucket, SourceForge and more
    • most easily installed with conda or pip
  • Vast majority of the classes you’ll need are likely to be freely available in open-source libraries.

Creating Your Own Custom Classes

  • Classes are new data types
  • Most applications you’ll build will commonly use either no custom classes or just a few
  • If you become part of a development team in industry, you may work on applications that contain hundreds, or even thousands, of classes

Inheritance

  • When creating a new class, you can inherit the attributes (variables) and methods (the class version of functions) of a previously defined base class (also called a superclass)
  • New class is called a derived class (or subclass)
  • You then customize the derived class to meet the specific needs of your application

Polymorphism

  • Enables you to conveniently program “in the general” rather than “in the specific”
  • You send the same method call to objects possibly of many different types
  • Each object responds by “doing the right thing”
  • So the same method call takes on “many forms,” hence the term “poly-morphism”

An Entertaining Case Study: Card-Shuffling-and-Dealing Simulation

  • You’ll use Matplotlib with attractive public-domain card images to display the full deck of cards both before and after the deck is shuffled

Data Classes

  • Python 3.7’s new data classes help you build classes faster by using a more concise notation and by autogenerating portions of the classes

Other Concepts Introduced in This Chapter

  • How to specify that certain identifiers should be used only inside a class and not be accessible to clients of the class
  • Special methods for creating string representations of your classes’ objects and specifying how objects of your classes work with Python’s built-in operators (a process called operator overloading)
  • An introduction to the Python exception class hierarchy and creating custom exception classes
  • Testing code with the Python Standard Library’s doctest module
  • How Python uses namespaces to determine the scopes of identifiers

©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 5 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.

DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.