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 ๐
Mission
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.
Attributes
Every book should have:
title
author
description
publication date
isbn
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.
__init__ ๐ช
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 ๐ฅณ
Functional Requirements
Usually methods will be related to at least one of your attributes.
For every Book, we should be able to:
checkout
one 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.checkin
one 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-outadd 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 copies_available
by 1
.
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 check_in
action.
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 copies_available
by 1
.
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
Summary
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)