Let’s explore the powerful object-oriented programming concepts available to us in Python!
This article is part of the course “Object-Oriented Python — for Beginners”. If you’re looking for a more beginner-oriented “Introduction to Python” course, I created one just for you! This course takes things a step further and delves deep into object-oriented Python. Earlier we saw how the
tuple data type is used to create a record. The
class can do the same, but it also allows us to bundle functions alongside the data.
Python is an object-based programming language. Every data type, and every variable, is an object. But Python does not require us to use the object-oriented programming paradigm, we’ve done just fine so far without writing any classes.
Writing Classes in Python
Object-oriented programming has its own vocabulary which you’ll need to become familiar with:
- Class: a class is the computer code which defines the blueprint for objects. A class is a piece of code, often quite complex, written by a programmer like you.
Let’s define a
- On line 5, we initiate the Book class blueprint.
- On line 10,
__init__is the initializer which allows the instance variables of a new object to be initialized using the arguments given.
- On lines 14–18 we initialize some instance variables. Some receive the value given during creation as arguments. Others are initialized to default values.
- We also define two instance methods,
set_note()and `Object: an object is something that you can manipulate with your program. It’s usually stored within a variable. Programmers create objects based on the blueprints defined in classes.
What’s going on here?
- First, on line 32, I create an instance of the class Book named
mybookand pass some values to the initializer
- Then I