Understanding Why You Need Classes
00:00 Object-oriented programming in Python means working with classes. To understand both object-oriented programming and classes, we need to understand why we might use them.
00:11 When you’re programming a larger application that works with data, maybe different kinds of data, these questions may arise: How do you store the data while the program’s running? Do you keep it in a list?
00:24 Do you keep it in a dictionary? Do you just make a load of variables? How do you store that? Where do you write the code? So if your code operates on data, where do you place that code?
00:35 Do you just keep all the functions close by? Do you just put it all in one big file? Do you separate out the different types of data into different types of file?
00:45 How do you operate on that data? So do you just create copies of lists? How do you know whether one dictionary represents one kind of data or another? Do you have some kind of check to make sure if they have certain properties?
00:57 And where do you put that code that operates on certain data? Again, do you spin that up into files? Do you just keep it all together with comments? These are all things that classes can really help with.
01:10 Now again, classes really are to make our life easier as a developer. It doesn’t mean you have to use them everywhere, but there are many situations in which classes can be really helpful to make your code easier to understand, easier to reason about, and easier to write.
01:28
Say you have three people here, and you have data associated with these people. In this example, they’re just kept in lists. So for instance, you have kirk
here, and you can tell by the list what’s going on.
01:41 The first item seems to be the name, the full name. The second item is probably the age. The third item is the role. And the fourth item, we’re not really sure what it is. Maybe it’s an ID number.
01:55
Now that seems to be true for the second item as well, where you have spock
. You have the name first, then the age, role, and some kind of ID number.
02:05 The last one, though, there’s something odd going on. It seems to go straight from the name to the role. Now, this can present a very tricky problem if you’re storing your data that way, because how do you know immediately whether items are missing?
02:19 You could look at the length, or you could check that the second item is a number. That said, those types of operations are very prone to errors because maybe your list size changes, or you have an extra field, and where do you put that field?
02:32 Things can get messy very quickly.
02:36
Not to mention when you’re actually trying to reference this data, looking at the code that references a list, it just says kirk[1]
. That doesn’t really tell you a lot about what’s happening here.
02:51
And as you’ve seen before, the result is that for kirk
and spock
, you get 34
and 35
, which seem like reasonable ages.
02:59
But then for mccoy
, you’re getting "Chief Medical Officer"
, which is clearly not an age. Whereas if you had used classes—you’re going to get into actually how do that later on in the course—but if you had used classes, the same operation would look something like this.
03:15
You have kirk.age
, and this immediately tells you, I’m getting the age from kirk
. This returns 34
, 35
in the case of spock
, and in mccoy
, it will give you an AttributeError
because it’s saying, I don’t have the age from mccoy
. Whereas with the list, it would just return a string value, and it’s not really telling you anything about the object itself, the person that it represents.
03:39 Let’s look at another example, one that may be a bit more practical. Say you are drawing things or you’re just keeping track of positions in two-dimensional space.
03:49
You might have an x1
variable and a y1
variable just to track the x
and y
position.
03:56
You may decide to keep this in a tuple, the first item being the x
position and the second item being the y
position. Now for simple operations, this might work just fine.
04:08
You index point[0]
, and you get 1
. That’s the x
. And you index point[1]
, and you get 2
. That’s the y
.
04:16
But even still, looking at this code, you’re not really sure what’s going on. Maybe point
is actually a list of different points, and point[0]
means the first point, and point[1]
means the second point.
04:27 If someone’s not familiar with your code, they might get confused while looking at it, and you yourself might get confused, because it’s very common for programmers to write something, come back a week later, and not have any memory of what they’ve wrote.
04:40
So do yourself a favor in these kinds of situations and use objects, where your code can look like this. point.x
is 1
. point.y
is 2
.
04:51
Immediately, this code is much less ambiguous because you can see that you are referencing the .x
attribute of point
and the .y
attribute of point
.
05:02 So now you have some idea of why you need classes. They’re a convenient structure to store and operate on data. It makes it easier to reference stuff in code, and it makes it easier to read the code and understand what it’s doing.
05:16 It’s also a convenient structure to encapsulate code, as you’ll come to see later in the course. It gives you somewhere to place all this code that is related to this type of data. In a nutshell, you need classes to make your life as a developer easier. Now, I’ll say this one last time: you can overdo classes.
05:36 Classes aren’t for every single situation. The great thing about Python is that you can use a bit of object-oriented programming and a bit of other types of programming as well.
05:46 If classes and object-oriented programming make sense for your current problem, go ahead and use them. But if you don’t feel like they make sense, then experiment without using them.
05:56 The key is to be able to use them if you want to and to understand all the great things that they can bring to you as a developer.
Become a Member to join the conversation.