Classes
Classes are a piece of code that specifies both the data and behavior for an object. You can think of a class like a blueprint for a house with each instance of that blue print being a construction of that house.
There are two types of things that can be found within a class: methods and attributes.
- Methods are the functions contained within a class
- Attributes are the variables contained within a class
Creating our First Class
Imagine you are an interesting person who goes on a lot of road trips. But you are also a boring person who tracks their road trips with python. One thing you could do is create a class to model your road trips.
For example, you could look at is how long it takes to go a certain distance. In order to know that, we would need to know how fast the car is going (on average) and how many miles the car has to go.
We are going to add a method to our class that determines the duration of a roadtrip based on the distance you plan on traveling and how fast you will be driving (on average).
class RoadTrip:
# our first method
def duration(self, distance, speed):
return (distance/speed)*60
Method
A method is a function attached to a class. In order to call a method, we first have to create an instance of the class.
Remember how we said a class is like a blueprint and an instance is like a house built from that blueprint? Well, map that logic onto road trips somehow because we are make an instance of a trip from our roadtrip class:
trip1 = RoadTrip()
Actually, calling a method is actually quite easy. In our case, all we have to do is the following:
trip1.duration(20, 20)
Attributes
But what if you want data attached to your class? You would use attributes. Attributes are like variables specific to your class or instance.
Instance attributes are attributes that belong a specific instance, not to the class in general. This is different from a class attribute that would be common across all given instances.
For example, how far you drive on your RoadTrip
and how fast you go will depend on each trip, but since you have no friends, a class attribute would be that you always drive alone.
In order to make instance attributes, we need the __init__
method. This is called the constructor method and it is a special method specifically for creating and storing attributes that are specific to a given instance.
See below for how to define our attributes:
class RoadTrip:
# a class attributes i.e. something that is true on all
# of your road trips
friends = 0
# init lets you set up instance attributes
def __init__(self, speed, distance):
self.speed = speed
self.distance = distance
You can also access instance attributes and use them in a method:
class RoadTrip:
# a class attributes i.e. something that is true on all
# of your road trips
friends = 0
# init lets you set up instance attributes
def __init__(self, speed, distance):
self.speed = speed
self.distance = distance
# our first method
def duration(self):
return (self.distance/self.speed)*60
We can also give default values to an instances attribute. Let's say that you typically drive a mazada. That could be a default value for a car attribute, but you could get in a different car, so technically you could override that value.
class RoadTrip:
# a class attributes i.e. something that is true on all
# of your road trips
friends = 0
# init lets you set up instance attributes
def __init__(self, speed, distance):
self.speed = speed
self.distance = distance
self.car = "Mazada"
# our first method
def duration(self):
return (self.distance/self.speed)*60
If you were to look run the following lines, you would see that the car defaults to Mazada for each trip instance:
# creating two instances
trip1 = RoadTrip(20, 20)
trip2 = RoadTrip(65, 120)
# see the car attribute for both
print(trip1.car)
print(trip2.car)
You could dynamically change these attributes if you want. Let's say you make a friend and then drive their car:
# creating two instances
trip1 = RoadTrip(20, 20)
trip2 = RoadTrip(65, 120)
# you made a friend for the second trip
trip2.car = "Benz"
trip2.friends = 1
print(trip2.car)
print(trip2.friends)
So What is Self?
So far you might have noticed that we are using the term self quite a bit and have yet to explain what it is.
Put simply but perhaps not clearly, self represents the instance of the class, allowing you to access variables, attributes, and methods of a defined class.
You don't have to call it self— though you probably should since everyone else does — you can call it whatever you want. I actually think this_trip is a better keyword since it more closely hews to our RoadTrip analogy.
Let's add another method to our class. One that describes our trip.
class RoadTrip:
# a class attributes i.e. something that is true on all
# of your road trips
friends = 0
# init lets you set up instance attributes
def __init__(this_trip, speed, distance):
this_trip.speed = speed
this_trip.distance = distance
this_trip.car = "Mazada"
# our first method
def duration(this_trip):
return (this_trip.distance/this_trip.speed)*60
# our second method
def describe(this_trip):
duration = this_trip.duration()
print("""We are going {} miles per hour for {} miles.
It will take {} minutes.""".format(
this_trip.speed,
this_trip.distance,
duration ))
trip = RoadTrip(20,20)
trip.describe()
When we change the "self" keyword to "this trip" it hopefully becomes a little clearer than the self keyword represents the instance of the class.