Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Dancing with Python

You're reading from   Dancing with Python Learn to code with Python and Quantum Computing

Arrow left icon
Product type Paperback
Published in Aug 2021
Publisher Packt
ISBN-13 9781801077859
Length 744 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Robert S. Sutor Robert S. Sutor
Author Profile Icon Robert S. Sutor
Robert S. Sutor
Arrow right icon
View More author details
Toc

Table of Contents (29) Chapters Close

Preface 1. Chapter 1: Doing the Things That Coders Do 2. Part I: Getting to Know Python FREE CHAPTER
3. Chapter 2: Working with Expressions 4. Chapter 3: Collecting Things Together 5. Chapter 4: Stringing You Along 6. Chapter 5: Computing and Calculating 7. Chapter 6: Defining and Using Functions 8. Chapter 7: Organizing Objects into Classes 9. Chapter 8: Working with Files 10. PART II: Algorithms and Circuits
11. Chapter 9: Understanding Gates and Circuits 12. Chapter 10: Optimizing and Testing Your Code 13. Chapter 11: Searching for the Quantum Improvement 14. PART III: Advanced Features and Libraries
15. Chapter 12: Searching and Changing Text 16. Chapter 13: Creating Plots and Charts 17. Chapter 14: Analyzing Data 18. Chapter 15: Learning, Briefly 19. References
20. Other Books You May Enjoy
21. Index
Appendices
1. Appendix A: Tools 2. Appendix B: Staying Current 3. Appendix C: The Complete UniPoly Class
4. Appendix D: The Complete Guitar Class Hierarchy
5. Appendix E: Notices 6. Appendix F: Production Notes

1.10 Objects and classes

Let’s return to cooking. I recently ran out of bay leaves in our kitchen. Bay leaves are used to flavor many foods, most notably soups, stews, and other braised meat dishes. Okay, I thought, I’ll just order some bay leaves online. I found four varieties:

  • Turkish or Mediterranean bay leaves, from the Bay Laurel tree,
  • Indian bay leaves,
  • California bay leaves, and
  • Caribbean bay leaves.

I eventually discovered that what I wanted was Turkish bay leaves. I ordered six ounces. When they arrived, I realized that I had enough to last the rest of my life.

Let’s get back to the varieties. While numbers and strings are built into most programming languages, bay leaves most certainly are not. Nevertheless, I want to create a structure to represent bay leaves in a first-class way. Perhaps I’m building up a collection of ingredient objects to use in digital recipes.

Before we consider bay leaves in particular, let’s think about leaves in general. Without worrying about syntax, we define a class called Leaf. It has two properties: is_edible and latin_name.

Once I create a Leaf object, you can ask if it is edible and what its Latin name is. Depending on the programming language, you can either examine the property directly or use a function on an object of class Leaf to get the information.

A function that operates on an object of a class is called a method.

For most users of the class, it is best to call a method to get object information. I do not want to publish to the world how I store and manipulate that data for objects. If I do this, I, as the class designer, am free to change the internal workings. For example, maybe I initially store the Latin names as strings, but I later decide to use a database. If a user of Leaf assumed I always had a string present, their code would break when I changed to using the database.

This hiding of the internal workings of classes is called encapsulation.

Now we define a class called BayLeaf, and we make it a child class of Leaf. Every BayLeaf object is also a Leaf. All the methods and properties of Leaf are also those of BayLeaf. Leaf is the parent class of BayLeaf.

Moreover, I can redefine the methods in BayLeaf that I inherit from Leaf to make them correct or more specific. For example, by default, I might define Leaf so that an object of that type always returns false when asked if it is edible. I override that method in BayLeaf so that it returns true.

I can add new properties and methods to child classes as well. Perhaps I need methods that list culinary uses, taste, and calories per serving. These are appropriate to objects of BayLeaf because they are edible, but not to all objects in class Leaf. Every object that is a bay leaf should be able to provide this information.

Continuing in this way, I define four child classes of BayLeaf: TurkishBayLeaf, IndianBayLeaf, CaliforniaBayLeaf, and CaribbeanBayLeaf. These can provide specific information about taste, say, and this can vary among the varieties.

In many languages, a class can have at most one parent. In some, multiple inheritance is allowed, but the languages provide special rules for properties and methods. That’s especially important when there are name collisions; two methods from different parents may have the same name but different behavior. Which method is called? For an analogy, consider the rules of genetics that determine the eye color of a child.

If we define the Herb class, then we can draw this class hierarchy. The arrows point from the class parents to their children.

Class hierarchy for Leaf and BayLeaf
Figure 1.6: Class hierarchy for Leaf and BayLeaf

Exercise 1.11

My brother likes bay rum cologne, which is made from Caribbean bay leaf. In which classes would you place a method that tells you whether you can make cologne from a variety of bay leaf? Where would you put the default definition? Where would you override it?

In this example involving bay leaves, the methods returned information. If I create a class for polynomials that all use the same variable, then I would likely define methods for addition, subtraction, negation, multiplication, quotient and remainder, degree, leading coefficient, and so forth.

In this section, I discussed what we call object-oriented programming (OOP) as it is implemented in languages like Python, C++, Java, and Swift. The terms superclass and subclass often replace parent class and child class, respectively. Once you are familiar with Python’s style of OOP, I encourage you to look at alternative approaches, such as how JavaScript does it.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime