How to Use Python 3 to Create Well-Designed Scripts


show more Applying polymorphism to classes provides you with in-depth training on Developer. Taught by Bill Weinman as part of the Python 3 Essential Training show less
please wait ...

Applying polymorphism to classes

Polymorphism is the practice of using one object of one particular class as if it were another object of another class. Let's take a look at how this is done in Python. This is actually something that Python is very good at. We'll make a working copy of classes.py and we'll call it classes-working.py. We'll open that and we see we have our Duck class that has quack and walk in it. And we'll go ahead and create a Dog class and Dog will have bark.

And fur. The dog has brown and white fur. Now, we'll go ahead and create a dog object called fido = Dog and fido.bark and fido.fur. So now we have a Dog object and a Duck object.

And if we go and run this, we see that the Duck quacks and walks like a duck. And the Dog barks and has brown and white fur. So these are two separate and distinct objects and they have two separate and distinct interfaces. If we want to be able to use them polymorphically, we need to make sure that they have a common interface. And so the Duck class, w...

Applying polymorphism to classes
Video duration: 7m 13s 6h 36m Beginner

Viewers:

Applying polymorphism to classes provides you with in-depth training on Developer. Taught by Bill Weinman as part of the Python 3 Essential Training

Subjects:
Developer Web
Software:
Python
Author:
please wait ...