Now that we've used objects and know that everything in Python is an object, it's time to define what an object is:
Objects and object-oriented programming are concepts that became popular in the early 1990s. Early computer languages, like C, did not have the concept of an object. However, it turned out that objects are an easy to understand paradigm for humans - it can be used to model many real-life situations. Most, if not all, new languages have the concept of objects these days. So what you're about to learn will conceptually apply to other languages too.
Since objects are the building blocks of the Python language, it's only logical that you can create objects yourself too. If you want to create your own type of object, you first need to define what methods it has and what data it can hold. This blueprint is called a class.
All objects are based on a class. When we create an object, we call this 'creating an instance of a class'. Strings, numbers, and even booleans are instances of a class too. Let's explore with the built-in function type:
>>> type('a') <class 'str'> >>> type(1) <class 'int'> type(True) <class 'bool'>
Apparently, there are classes called
bool. These are some of Python's native classes, but we can build our own class too!
No tutorial is complete without a car analogy, so let's make a class that represents a car. It's a lot to type in and you have to start over on each mistake. Feel free to try, but if you want to take a shortcut I understand. Just copy and paste the following into your Python REPL:
class Car: speed = 0 started = False def start(self): self.started = True print("Car started, let's ride!") def increase_speed(self, delta): if self.started: self.speed = self.speed + delta print('Vrooooom!') else: print("You need to start the car first") def stop(self): self.speed = 0 print('Halting')
Don't worry, we'll go over this step by step, but let's first create and use an object of type Car:
>>> car = Car() >>> car.increase_speed(10) You need to start the car first >>> car.start() Car started, let's ride! >>> car.increase_speed(40) Vrooooom! >>> _
An object is always an instance of a class. One class can have many instances. We just created an instance of class
Car(), and assigned it to the (lowercase) variable
car. Creating an instance looks like calling a function ; you'll learn why later on.
Next, we call one of the methods on our car object: trying to increase its speed while it's not started yet. Oops! Only after starting the car, we can increase its speed and enjoy the noise it makes.
Now let's go over our Car class step by step:
In the definitions of these methods, we encounter something peculiar: they all have an argument called self as their first argument.
Honestly, this is one of Python's less elegant language constructs if you ask me.
Remember when we were calling the methods on our car object, like
car.start()? We didn't have to pass the
self variable, even though
start() is defined as
start(self) inside the class.
This is what's happening:
So only inside the class definition, we use
self to reference variables that are part of the instance. To modify the
started variable that's part of our class, we use
self.started and not just
self, it's made abundantly clear that we are operating on a variable that's part of this instance and not some other variable that is defined outside of the object and happens to have the same name.
Since a class is just a blueprint, you can use it to create multiple objects, just like you can build multiple identical-looking cars. They all behave similarly, but they all have their own data that is not shared between objects:
>>> car1 = Car() >>> car2 = Car() >>> id(car1) 139771129539104 >>> id(car2) 139771129539160
We created two car objects here, car1 and car2, and used the built-in method id() to get their ids. Each object in Python has a unique identifier, so we just proved that we created two different objects from the same class. We can use them independently:
>>> car1.start() Car started, let's ride! >>> car1.increase_speed(10) 'Vrooom!' >>> car1.speed 10 >>> car2.speed 0
We just started
car1 and increased its speed, while
car2 is still halted. Inspection of the speeds confirms these are different cars with different states!
If you liked this page, please share it with a fellow learner: