DEV Community

Ghameerah McCullers
Ghameerah McCullers

Posted on


Creating a Book Class ๐Ÿ“š

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 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:

publication date
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.

Book constructor

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
New Book created in Python class

In this example we call Book(...), NOT __init__.
We can also add a second book to our collection by updating our code

Another created Book

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-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.

Checkout method

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.
Check in

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

two more actions

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

copies left

  • 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)

Advice For Junior Developers

Advice from a career of 15+ years for new and beginner developers just getting started on their journey.