The Case for Object-Oriented Programming
01:17 Wouldn’t it be great if I could group these kinds of things together? Well, that’s the idea behind the object-oriented programming methodology. You use the language’s features to group data and operations on that data together. Of course, you could do this just by keeping everything together in the same file, but it would be helpful to have the compiler enforce some of the rules.
There’s another reason for the object-oriented approach. Let’s go back into the REPL and look at some data. Here. I’ve got a person. The
person dictionary has two key-value pairs corresponding to the first and last name. Let’s create somebody else.
This is similar but slightly different. I’ve got a third property, the ID. Logically,
"Yennefer" is both an employee and a person. If you want the concept of a person to also have a middle name, using data structures like this means making changes in two places.
Say I had a function that expected a dictionary with keys for first and last name. I could pass either a person or an employee and it would work, but if I misspelled
"first" in Yennefer’s dictionary, it would fail.
The employee is a person, so everything in
person should also be an employee. This mechanism is known as inheritance, where
employee inherits the person’s attributes. If you did it this way, any changes to the person, like the addition of a middle name, would also show up in the employee as well.
03:44 Examining the object, you see that it is a class. A class is what Python and most object-oriented languages calls the template that describes the structure of the data. The class doesn’t have any data in it.
p object has attributes, which you access using dot notation. The fact that I called that an attribute is quite possibly a gross oversimplification bordering on a lie, but the details about how it works underneath aren’t important.
05:15 You’ll sometimes hear people speaking about this loosely, but the strict definition is a function doesn’t belong to an object, while a method does. Just like a function, you can call a method using parentheses.
06:26 In addition to the regular kind of members, Python also has special members, sometimes called magic members or dunder members. Dunder is short for double underscore, as these special members have a double-underscore prefix and suffix.
Get used to me calling this dunder. Not only is it the slang most Python programmers use, it’s much more fun to say.
.__str__() is a special method that Python calls on the object when you convert it to a string.
PosixPath class is actually built using inheritance. There is a base class called
Path that is a general implementation of path-like functionality. The
WindowsPath classes extend
Path, meaning they are
Path classes having all the same attributes and methods as
WindowsPath implement Unix- and Windows-specific features, built on top of the base class.
07:53 Let’s summarize. Why might we use object-oriented coding? Code often relates to real-world things—people and file paths, for example. Keeping the data for these things and the operations on them together just kind of makes sense. By grouping things in classes, you can reuse the code.
employee have the first and last name attributes, I can operate on them as if they’re equivalent without worrying about what else they contain. This leads to a degree of flexibility.
09:19 A related fancy academic term for this is polymorphism: poly meaning many and morph meaning forms. A common example of this in Python—and Unix in general—is file-like behavior. If you implement the file protocol methods, there are all sorts of libraries that can use your object, which don’t care whether you’re reading and writing to the local disk or to some remote S3 server.
Become a Member to join the conversation.