Creating a Book Class 📚
Tags: python, objectorientedprogramming, code, oop
Published: 2022-05-05 17:32:03

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/opvdg1ardz1zr74bkha5.png) 


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


![Book constructor](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1mu57vu5crgxmj3zx9ad.png)

 
**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ueouycjdmvk3t39b9w4p.png) 
 

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/on9xmr8kvyry155i2czy.png)

 
## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3jahg637xg0x0xbsg853.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/si58u19fmt80qvv493zy.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6f28qcly26vrpjx45y2e.png)
 


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: 


![title](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ksg9dgrxob9w800bgrki.png)

- Will output "The Power of Now" in terminal


 ![kyrie](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/958od0ji1pezm5hcgvxf.png)
 
- Will output "Ajh" and "Kyrie" to the terminal


![copies left](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ba84tougnusuu881m1lb.png)
 
- Will output "7" to the terminal


![mindful](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7walja8i9z7c7uu50df0.png)
 
- Will output "10" to the terminal


![mindful](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o3y9bh9mxgsovjgzrknu.png)
- Will output "Ajh", "Malcolm" and "Mansa Mua" to the terminal


## Summary

Our final Book Class


![final](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1wnazlnlko7scc5ri8vb.png)
 
What are some other actions or attributes we can add to a book? Comment below if you think of a cool related attribute. 😄