One thing that attracts me to programming is the ability to represent real-life objects in code. In this article, you'll learn how to create a Book class using Python. Let's learn about the core functionality of Python objects and classes by creating a fun Book class! 🐍🖥
Python exercise 1 😄
Fact: Libraries have a lot of books.
If we want to make a program that tracks all the books in a library, we can make the process much more efficient by making a class for a book, much like a template. That way we can more easily make and keep track of all the different books.
Every book should have:
number of copies available to check-out
list of names of people who currently have this book checked-out
What is a Book? (to Python) 🧑🏾💻
How do we communicate to Python to tell it what a book is?
Think of attributes as the adjectives and nouns associated with a book?
Examples of a book may include...
- Title: "The Power of Now",
- Author: "Eckhart Tolle",
- Description: "The Power of Now: A Guide to Spiritual Enlightenment",
- ISBN: "9781682300688",
- Published Date: "1997",
- Copies Available: 8,
- Who currently has one checked out: "Ajh"
Let's start by adding these simple attributes to our Book class. We'll create the class Book and create an __init__ method.
Quick derail because I can't stress the importance of this method we call the constructor. A constructor defines what our Books will be when they are first introduced to the world, the program and Python.
self will refer to a specific book that we create. self.title refers to that specific book's title.
We can create a new book by calling our constructor. We call and the constructor picks up, we ask "May I create one new book using these attributes." The constructor then grants the request and creates a new Book.
Notice that when we create a new book, we say it like this
In this example we call Book(...), NOT __init__.
We can also add a second book to our collection by updating our code
I Got A Instance 🍪
So far we've created two instances of our Book class. An instance is a single occurrence of an object. In our above examples we had two instances of a Book but their attributes are specific to each of the respective Books.
Methods: What A Library Book Can Do 🥳
Usually methods will be related to at least one of your attributes.
For every Book, we should be able to:
checkoutone book to a single person: This feature should update the number of copies available to check-out, and the list of names of people who currently have this book checked-out.
checkinone book from a single person: This feature should update the number of copies available to check-out, and the list of names of people who currently have this book checked-out
add more copies of this book: This feature should update the copies available
remove more copies of this book: This feature should update the copies available
These actions might change some of the attributes we already gave our Book or necessitate that we add new attributes. Let's first update our Book class to be able to be checked out. We'll create a
checkout method inside of our class.
The self parameter refers to the specific book we're attaching this method to. The name parameter refers to the specific name of the person checking the book out. In this method we add the name to the
checked_out_by list by appending it and then we decrease the
Not too fast 🚦
To complete this task we will be using object oriented design, though it is interchangeable with object oriented programming.
What is OOP? Object-oriented programming is a computer programming model that organizes software design around data, or objects, rather than functions and logic. An object can be defined as a data field that has unique attributes and behavior. Think of OOP as creating a cookie-cutter template for your created data sets. OOP allows you to create new data types in your code that otherwise wouldn't be available to you.
Back to Business 😄
Let's add the functionality of checking in a book to our class. This going to be the opposite action of our
checkout action, instead we'll be building the
Notice in this method we remove the name from the
checked_out_by list by calling
.remove() and passing in the
name and then we increase the
Next, we'll need to add a method that:
- Adds more copies to the shelf
- Removes copies from the shelf
Since we have
copies_available as a number we can easily manipulate it's value.
See it in Action! 🎬 Here are some examples of the actions we can now do our book:
- Will output "The Power of Now" in terminal
- Will output "Ajh" and "Kyrie" to the terminal
- Will output "7" to the terminal
- Will output "10" to the terminal
- Will output "Ajh", "Malcolm" and "Mansa Mua" to the terminal
Our final Book Class
What are some other actions or attributes we can add to a book? Comment below if you think of a cool related attribute. 😄
Top comments (0)