Book Image

Learning Object-Oriented Programming

By : Gaston C. Hillar
Book Image

Learning Object-Oriented Programming

By: Gaston C. Hillar

Overview of this book

Table of Contents (16 chapters)
Learning Object-Oriented Programming
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Working with duck typing in Python


We will use the Animal base class to generalize the requirements for animals. First, we will specialize the base class in two subclasses: Dog and Frog. Then, we will create a Party class that will be able to work with instances of any Animal subclass through duck typing. We will work with a party of dogs and a party of frogs.

Then, we will create a HorseDeeJay class and generate a subclass of the Party class named PartyWithDeeJay. The new subclass will work with instances of any Animal subclass and any instance that provides the properties and methods declared in the HorseDeeJay class through duck typing. We will work with the party of dogs with a DJ.

Declaring a base class that defines the generic behavior

We will create many classes that require the following import statement:

import random

Now, we will declare a base class named Animal:

class Animal:
    dance_characters = ""
    spelled_sound_1 = ""
    spelled_sound_2 = ""
    spelled_sound_3 = ""

   ...