René's URL Explorer Experiment


Title: The Case for Object-Oriented Programming (Video) – Real Python

Open Graph Title: The Case for Object-Oriented Programming – Real Python

Description: In the previous lesson, I gave an overview of the course. In this lesson, I’ll make the argument for the use of object-oriented coding. Who doesn’t love a good argument? Let’s start by diving right into some code. Consider this small function. It…

Open Graph Description: In the previous lesson, I gave an overview of the course. In this lesson, I’ll make the argument for the use of object-oriented coding. Who doesn’t love a good argument? Let’s start by diving right into some code. Consider this small function. It…

Opengraph URL: https://realpython.com/videos/why-oop-python/

X: @realpython

direct link

Domain: realpython.com


Hey, it has json ld scripts:
  {
    "@context": "https://schema.org",
    "@type": "VideoObject",
    "name": "The Case for Object\u002DOriented Programming",
    "description": "In the previous lesson, I gave an overview of the course. In this lesson, I’ll make the argument for the use of object\u002Doriented coding. Who doesn’t love a good argument? Let’s start by diving right into some code. Consider this small function. It…",
    "thumbnailUrl": ["https://files.realpython.com/media/Class-Concepts-Object-Oriented-Programming-in-Python_Watermarked.6cf327c51434.jpg"],
    "uploadDate": "2023-09-05T14:00:00+00:00",
    "duration": "PT9M55S",
    "embedUrl": "https://player.vimeo.com/video/857918105",
    "potentialAction": {
      "@type": "SeekToAction",
      "target": "https://realpython.com/videos/why-oop-python/#t={seek_to_second_number}",
      "startOffset-input": "required name=seek_to_second_number"
    }
  }
  

authorReal Python
twitter:cardsummary_large_image
twitter:imagehttps://files.realpython.com/media/Class-Concepts-Object-Oriented-Programming-in-Python_Watermarked.6cf327c51434.jpg
og:imagehttps://files.realpython.com/media/Class-Concepts-Object-Oriented-Programming-in-Python_Watermarked.6cf327c51434.jpg
twitter:creator@realpython
og:typevideo.episode

Links:

https://realpython.com/
Start Herehttps://realpython.com/start-here/
Learn Python https://realpython.com/videos/why-oop-python/
Python Tutorials →In-depth articles and video courseshttps://realpython.com/search?kind=article&kind=course&order=newest
Learning Paths →Guided study plans for accelerated learninghttps://realpython.com/learning-paths/
Quizzes & Exercises →Check your learning progresshttps://realpython.com/quizzes/
Browse Topics →Focus on a specific area or skill levelhttps://realpython.com/tutorials/all/
Community Chat →Learn with other Pythonistashttps://realpython.com/community/
Office Hours →Live Q&A calls with Python expertshttps://realpython.com/office-hours/
Podcast →Hear what’s new in the world of Pythonhttps://realpython.com/podcasts/rpp/
Books →Round out your knowledge and learn offlinehttps://realpython.com/products/books/
Reference →Concise definitions for common Python termshttps://realpython.com/ref/
Code Mentor →BetaPersonalized code assistance & learning toolshttps://realpython.com/mentor/
Unlock All Content →https://realpython.com/account/join/
More https://realpython.com/videos/why-oop-python/
Learner Storieshttps://realpython.com/learner-stories/
Python Newsletterhttps://realpython.com/newsletter/
Python Job Boardhttps://www.pythonjobshq.com
Meet the Teamhttps://realpython.com/team/
Become a Tutorial Writerhttps://realpython.com/write-for-us/
Become a Video Instructorhttps://realpython.com/become-an-instructor/
Searchhttps://realpython.com/search
https://realpython.com/search
Joinhttps://realpython.com/account/join/
Sign‑Inhttps://realpython.com/account/login/?next=%2Fvideos%2Fwhy-oop-python%2F
https://realpython.com/courses/python-class-object/#team
Class Concepts: Object-Oriented Programming in Pythonhttps://realpython.com/courses/python-class-object/
Christopher Trudeauhttps://realpython.com/courses/python-class-object/#team
Recommended Tutorialhttps://realpython.com/python-classes/
Course Slides (.pdf)https://realpython.com/courses/python-class-object/downloads/python-class-object-slides/
Sample Code (.zip)https://realpython.com/courses/python-class-object/downloads/python-class-object-code/
Ask a Questionhttps://realpython.com/videos/why-oop-python/#discussion
https://realpython.com/feedback/survey/course/python-class-object/liked/?from=lesson-title
https://realpython.com/feedback/survey/course/python-class-object/disliked/?from=lesson-title
Transcripthttps://realpython.com/videos/why-oop-python/#transcript
Discussion (2)https://realpython.com/videos/why-oop-python/#discussion
00:00https://realpython.com/videos/why-oop-python/#t=0.54
In the previous lesson, I gave an overview of the course. In this lesson,https://realpython.com/videos/why-oop-python/#t=0.54
I’ll make the argument for the use of object-oriented coding.https://realpython.com/videos/why-oop-python/#t=4.39
Who doesn’t love a good argument?https://realpython.com/videos/why-oop-python/#t=7.9399999999999995
00:10https://realpython.com/videos/why-oop-python/#t=10.86
Let’s start by diving right into some code.https://realpython.com/videos/why-oop-python/#t=10.86
00:24https://realpython.com/videos/why-oop-python/#t=24.6
Consider this small function. It calculates the area of a circle,https://realpython.com/videos/why-oop-python/#t=24.6
taking radius as an argument.https://realpython.com/videos/why-oop-python/#t=27.98
00:33https://realpython.com/videos/why-oop-python/#t=33.24
Whatever units 3 is in, the area is 28 of those very same units.https://realpython.com/videos/why-oop-python/#t=33.24
For fun, let’s say light-years.https://realpython.com/videos/why-oop-python/#t=38.46
00:41https://realpython.com/videos/why-oop-python/#t=41.91
Consider if I wanted to do other things with that circle.https://realpython.com/videos/why-oop-python/#t=41.91
I might create a dictionary with some data describing it.https://realpython.com/videos/why-oop-python/#t=44.91
This dictionary describes both the circle’s radius and its color.https://realpython.com/videos/why-oop-python/#t=48.06
00:52https://realpython.com/videos/why-oop-python/#t=52.89
If I want to calculate the area,https://realpython.com/videos/why-oop-python/#t=52.89
00:59https://realpython.com/videos/why-oop-python/#t=59.01
I reference the radius key in the dictionary and pass it into the function.https://realpython.com/videos/why-oop-python/#t=59.01
Over time, my code might grow. I might add a function for perimeter.https://realpython.com/videos/why-oop-python/#t=63.81
01:08https://realpython.com/videos/why-oop-python/#t=68.74
I might also add a square,https://realpython.com/videos/why-oop-python/#t=68.74
and passing the length of a square side into the circle_area() function wouldn’thttps://realpython.com/videos/why-oop-python/#t=70.4
really give me a logical result.https://realpython.com/videos/why-oop-python/#t=75.26
01:17https://realpython.com/videos/why-oop-python/#t=77.38
Wouldn’t it be great if I could group these kinds of things together? Well,https://realpython.com/videos/why-oop-python/#t=77.38
that’s the idea behind the object-oriented programming methodology.https://realpython.com/videos/why-oop-python/#t=82.06
You use the language’s features to group data and operations on that datahttps://realpython.com/videos/why-oop-python/#t=86.22
together. Of course,https://realpython.com/videos/why-oop-python/#t=90.36
you could do this just by keeping everything together in the same file,https://realpython.com/videos/why-oop-python/#t=92.3
but it would be helpful to have the compiler enforce some of the rules.https://realpython.com/videos/why-oop-python/#t=95.98
01:40https://realpython.com/videos/why-oop-python/#t=100.47
There’s another reason for the object-oriented approach.https://realpython.com/videos/why-oop-python/#t=100.47
Let’s go back into the REPL and look at some data. Here.https://realpython.com/videos/why-oop-python/#t=103.37
I’ve got a person.https://realpython.com/videos/why-oop-python/#t=107.75
The person dictionary has two key-value pairs corresponding to the first andhttps://realpython.com/videos/why-oop-python/#t=109.3
last name. Let’s create somebody else.https://realpython.com/videos/why-oop-python/#t=113.96
02:06https://realpython.com/videos/why-oop-python/#t=126.58
This is similar but slightly different. I’ve got a third property,https://realpython.com/videos/why-oop-python/#t=126.58
the ID. Logically, "Yennefer" is both an employee and a person.https://realpython.com/videos/why-oop-python/#t=131.01
If you want the concept of a person to also have a middle name,https://realpython.com/videos/why-oop-python/#t=136.29
using data structures like this means making changes in two places.https://realpython.com/videos/why-oop-python/#t=139.88
02:24https://realpython.com/videos/why-oop-python/#t=144.78
This is the second key concept of object-oriented coding:https://realpython.com/videos/why-oop-python/#t=144.78
structuring data in a way that is reusable.https://realpython.com/videos/why-oop-python/#t=148.79
You also want some rudimentary verification as well.https://realpython.com/videos/why-oop-python/#t=151.93
02:35https://realpython.com/videos/why-oop-python/#t=155.25
Say I had a function that expected a dictionary with keys for first and lasthttps://realpython.com/videos/why-oop-python/#t=155.25
name. I could pass either a person or an employee and it would work,https://realpython.com/videos/why-oop-python/#t=159.11
but if I misspelled "first" in Yennefer’s dictionary, it would fail.https://realpython.com/videos/why-oop-python/#t=164.17
02:48https://realpython.com/videos/why-oop-python/#t=168.33
Having the language ensure that can’t happen is helpful.https://realpython.com/videos/why-oop-python/#t=168.33
02:52https://realpython.com/videos/why-oop-python/#t=172.89
So not only do you want data and its associated operations together,https://realpython.com/videos/why-oop-python/#t=172.89
but you want to be able to structure your data so it can be reused.https://realpython.com/videos/why-oop-python/#t=176.69
This is sometimes referred to using the phrase is a.https://realpython.com/videos/why-oop-python/#t=180.67
03:04https://realpython.com/videos/why-oop-python/#t=184.5
The employee is a person,https://realpython.com/videos/why-oop-python/#t=184.5
so everything in person should also be an employee.https://realpython.com/videos/why-oop-python/#t=187.1
This mechanism is known as inheritance, where employee inheritshttps://realpython.com/videos/why-oop-python/#t=190.89
the person’s attributes. If you did it this way,https://realpython.com/videos/why-oop-python/#t=195.72
any changes to the person, like the addition of a middle name, would also show uphttps://realpython.com/videos/why-oop-python/#t=199.39
in the employee as well.https://realpython.com/videos/why-oop-python/#t=203.61
03:25https://realpython.com/videos/why-oop-python/#t=205.89
Building code this way means better reuse and makes mistakes based on the formhttps://realpython.com/videos/why-oop-python/#t=205.89
of the data less likely.https://realpython.com/videos/why-oop-python/#t=209.69
Let’s go back to the REPL and look at an object like this in the standardhttps://realpython.com/videos/why-oop-python/#t=212.09
library.https://realpython.com/videos/why-oop-python/#t=215.48
03:40https://realpython.com/videos/why-oop-python/#t=220.01
This is the PosixPath object from the pathlib module.https://realpython.com/videos/why-oop-python/#t=220.01
03:44https://realpython.com/videos/why-oop-python/#t=224.93
Examining the object, you see that it is a class.https://realpython.com/videos/why-oop-python/#t=224.93
A class is what Python and most object-oriented languages calls the templatehttps://realpython.com/videos/why-oop-python/#t=228.09
that describes the structure of the data. The class doesn’t have any data in it.https://realpython.com/videos/why-oop-python/#t=232.93
03:57https://realpython.com/videos/why-oop-python/#t=237.75
It merely tells Python the form of the data and the methods that can be run uponhttps://realpython.com/videos/why-oop-python/#t=237.75
it. That’s a little white lie, but that’s close enough for now.https://realpython.com/videos/why-oop-python/#t=241.85
I’ll show you the details as the course goes on.https://realpython.com/videos/why-oop-python/#t=246.12
04:09https://realpython.com/videos/why-oop-python/#t=249.35
The thing that stores the data is an object,https://realpython.com/videos/why-oop-python/#t=249.35
which you get by instantiating the class. In Python (and inhttps://realpython.com/videos/why-oop-python/#t=251.98
most object-oriented languages),https://realpython.com/videos/why-oop-python/#t=256.85
you do this by calling the class with parentheses.https://realpython.com/videos/why-oop-python/#t=258.35
04:25https://realpython.com/videos/why-oop-python/#t=265.09
You may have seen this mechanism without thinking about what was going on.https://realpython.com/videos/why-oop-python/#t=265.09
PosixPath is a class. Using the parentheses on the class constructs an object.https://realpython.com/videos/why-oop-python/#t=269.22
04:34https://realpython.com/videos/why-oop-python/#t=274.61
In this case, that construction takes an argument, the name of a file.https://realpython.com/videos/why-oop-python/#t=274.61
The result of constructing the class is a new object instance,https://realpython.com/videos/why-oop-python/#t=279.1
which I’ve put in a variable named p.https://realpython.com/videos/why-oop-python/#t=282.49
04:47https://realpython.com/videos/why-oop-python/#t=287.05
The p object has attributes, which you access using dot notation.https://realpython.com/videos/why-oop-python/#t=287.05
The fact that I called that an attribute is quite possibly a grosshttps://realpython.com/videos/why-oop-python/#t=292.21
oversimplification bordering on a lie,https://realpython.com/videos/why-oop-python/#t=295.41
but the details about how it works underneath aren’t important.https://realpython.com/videos/why-oop-python/#t=298.01
05:02https://realpython.com/videos/why-oop-python/#t=302.23
Dot notation on a member gives its value—in this case, the name of the filehttps://realpython.com/videos/why-oop-python/#t=302.23
the object was constructed with. The object also has methods.https://realpython.com/videos/why-oop-python/#t=306.53
These are like functions attached to the object.https://realpython.com/videos/why-oop-python/#t=311.94
05:15https://realpython.com/videos/why-oop-python/#t=315.07
You’ll sometimes hear people speaking about this loosely,https://realpython.com/videos/why-oop-python/#t=315.07
but the strict definition is a function doesn’t belong to an object,https://realpython.com/videos/why-oop-python/#t=318.35
while a method does. Just like a function,https://realpython.com/videos/why-oop-python/#t=322.1
you can call a method using parentheses.https://realpython.com/videos/why-oop-python/#t=325.35
05:28https://realpython.com/videos/why-oop-python/#t=328.23
The .exists() method returns True if the file the PosixPath object is pointing tohttps://realpython.com/videos/why-oop-python/#t=328.23
exists. In my case, it does not.https://realpython.com/videos/why-oop-python/#t=333.39
05:37https://realpython.com/videos/why-oop-python/#t=337.52
The general term for both attributes and methods is members, as in the membershttps://realpython.com/videos/why-oop-python/#t=337.52
of the object or class.https://realpython.com/videos/why-oop-python/#t=342.48
You can see all the members by passing an object to the built-in dirs() function.https://realpython.com/videos/why-oop-python/#t=344.64
05:50https://realpython.com/videos/why-oop-python/#t=350.38
That’s quite a lot, isn’t it? If you scan through here,https://realpython.com/videos/why-oop-python/#t=350.38
you’ll find both the .name attribute and the .exists() method.https://realpython.com/videos/why-oop-python/#t=353.66
05:59https://realpython.com/videos/why-oop-python/#t=359.6
Okay, let’s review. I started with the PosixPath, which is a class.https://realpython.com/videos/why-oop-python/#t=359.6
I then constructed it by calling it with parentheses,https://realpython.com/videos/why-oop-python/#t=365.57
passing in "demo.py" as an argument.https://realpython.com/videos/why-oop-python/#t=368.68
06:11https://realpython.com/videos/why-oop-python/#t=371.92
This construction is called instantiation.https://realpython.com/videos/why-oop-python/#t=371.92
The constructor returns an instance object,https://realpython.com/videos/why-oop-python/#t=375.15
which I put in the variable named p.https://realpython.com/videos/why-oop-python/#t=378.18
The instance object has attributes and methods.https://realpython.com/videos/why-oop-python/#t=381.78
06:26https://realpython.com/videos/why-oop-python/#t=386.81
In addition to the regular kind of members,https://realpython.com/videos/why-oop-python/#t=386.81
Python also has special members, sometimes called magic members or dunderhttps://realpython.com/videos/why-oop-python/#t=388.89
members. Dunder is short for double underscore,https://realpython.com/videos/why-oop-python/#t=393.59
as these special members have a double-underscore prefix and suffix.https://realpython.com/videos/why-oop-python/#t=397.97
06:42https://realpython.com/videos/why-oop-python/#t=402.46
.__doc__ contains the docstring for the object. Yep,https://realpython.com/videos/why-oop-python/#t=402.46
you can actually get the documentation associated with the codehttps://realpython.com/videos/why-oop-python/#t=406.81
programmatically. It is kept as a special attribute on the object itself.https://realpython.com/videos/why-oop-python/#t=409.59
06:54https://realpython.com/videos/why-oop-python/#t=414.73
Get used to me calling this dunder.https://realpython.com/videos/why-oop-python/#t=414.73
Not only is it the slang most Python programmers use,https://realpython.com/videos/why-oop-python/#t=416.77
it’s much more fun to say. .__str__() is a special method that Pythonhttps://realpython.com/videos/why-oop-python/#t=419.94
calls on the object when you convert it to a string.https://realpython.com/videos/why-oop-python/#t=424.97
07:08https://realpython.com/videos/why-oop-python/#t=428.46
If you print out p or pass it to the str() string function,https://realpython.com/videos/why-oop-python/#t=428.46
Python calls this method, and whatever it returns is what gets printed orhttps://realpython.com/videos/why-oop-python/#t=432.82
converted.https://realpython.com/videos/why-oop-python/#t=437.64
07:20https://realpython.com/videos/why-oop-python/#t=440.37
The PosixPath class is actually built using inheritance.https://realpython.com/videos/why-oop-python/#t=440.37
There is a base class called Path that is a general implementation of path-likehttps://realpython.com/videos/why-oop-python/#t=444.48
functionality. The PosixPath and WindowsPath classes extend Path,https://realpython.com/videos/why-oop-python/#t=449.15
meaning they are Path classes having all the same attributes and methods ashttps://realpython.com/videos/why-oop-python/#t=454.54
Path. PosixPath and WindowsPath implementhttps://realpython.com/videos/why-oop-python/#t=459.58
Unix- and Windows-specific features, built on top of the base class.https://realpython.com/videos/why-oop-python/#t=463.3
07:48https://realpython.com/videos/why-oop-python/#t=468.62
They’re the employee to Path’s person.https://realpython.com/videos/why-oop-python/#t=468.62
07:53https://realpython.com/videos/why-oop-python/#t=473.2
Let’s summarize. Why might we use object-oriented coding?https://realpython.com/videos/why-oop-python/#t=473.2
Code often relates to real-world things—people and file paths, for example.https://realpython.com/videos/why-oop-python/#t=477.57
Keeping the data for these things and the operations on them together just kindhttps://realpython.com/videos/why-oop-python/#t=482.53
of makes sense. By grouping things in classes, you can reuse the code.https://realpython.com/videos/why-oop-python/#t=486.74
08:12https://realpython.com/videos/why-oop-python/#t=492.76
The common code to both Unix and Windows file descriptors should be in one place.https://realpython.com/videos/why-oop-python/#t=492.76
Only the stuff specific to Unix or Windows should be considered separately.https://realpython.com/videos/why-oop-python/#t=497.49
08:22https://realpython.com/videos/why-oop-python/#t=502.53
That way, if you find a bug in the common code,https://realpython.com/videos/why-oop-python/#t=502.53
you only have to fix it in one place. This is part of the DRY principle,https://realpython.com/videos/why-oop-python/#t=505.08
short for don’t repeat yourself.https://realpython.com/videos/why-oop-python/#t=510.27
08:33https://realpython.com/videos/why-oop-python/#t=513.7
Defining classes can provide an abstraction mechanism.https://realpython.com/videos/why-oop-python/#t=513.7
That means an object can have a well-known interface that the users of thathttps://realpython.com/videos/why-oop-python/#t=517.29
object just need to know in order to use it.https://realpython.com/videos/why-oop-python/#t=521.26
08:44https://realpython.com/videos/why-oop-python/#t=524.6
If both person and employee have the first and last name attributes,https://realpython.com/videos/why-oop-python/#t=524.6
I can operate on them as if they’re equivalent without worrying about what elsehttps://realpython.com/videos/why-oop-python/#t=529.22
they contain. This leads to a degree of flexibility.https://realpython.com/videos/why-oop-python/#t=533.09
08:57https://realpython.com/videos/why-oop-python/#t=537.87
You can build interfaces where the coder doesn’t really care about thehttps://realpython.com/videos/why-oop-python/#t=537.87
underlying implementation as long as the interface is understood.https://realpython.com/videos/why-oop-python/#t=541.13
You may have heard the phrase duck-typing in Python.https://realpython.com/videos/why-oop-python/#t=545.87
09:08https://realpython.com/videos/why-oop-python/#t=548.55
It refers to the fact that if it looks like a duck and talks like a duck,https://realpython.com/videos/why-oop-python/#t=548.55
you can use it like a duck, regardless of whether it is a swan inhttps://realpython.com/videos/why-oop-python/#t=552.35
disguise or not.https://realpython.com/videos/why-oop-python/#t=556.89
09:19https://realpython.com/videos/why-oop-python/#t=559.13
A related fancy academic term for this is polymorphism:https://realpython.com/videos/why-oop-python/#t=559.13
poly meaning many and morph meaning forms.https://realpython.com/videos/why-oop-python/#t=563.24
A common example of this in Python—and Unix in general—is file-likehttps://realpython.com/videos/why-oop-python/#t=567.07
behavior. If you implement the file protocol methods,https://realpython.com/videos/why-oop-python/#t=572.09
there are all sorts of libraries that can use your object,https://realpython.com/videos/why-oop-python/#t=574.98
which don’t care whether you’re reading and writing to the local disk or to somehttps://realpython.com/videos/why-oop-python/#t=577.82
remote S3 server.https://realpython.com/videos/why-oop-python/#t=582.07
09:44https://realpython.com/videos/why-oop-python/#t=584.23
You abstract the details away and just pass around your duck.https://realpython.com/videos/why-oop-python/#t=584.23
09:49https://realpython.com/videos/why-oop-python/#t=589.69
Enough abstractness and abstraction. Next up, you’ll write some code.https://realpython.com/videos/why-oop-python/#t=589.69
Aug. 22, 2024https://realpython.com/videos/why-oop-python/#comment-1ec40850-ecf6-45a0-a283-b2bc4f49c290
Aug. 22, 2024https://realpython.com/videos/why-oop-python/#comment-b05f24cd-a230-49c0-bcad-123f88fe2b84
Become a Memberhttps://realpython.com/account/join/
https://realpython.com/videos/python-class-object-overview/
Overviewhttps://realpython.com/courses/python-class-object/
https://realpython.com/lessons/python-class-creation/
Class Concepts: Object-Oriented Programming in Python (Overview) 03:22 https://realpython.com/videos/python-class-object-overview/
The Case for Object-Oriented Programming 09:55 https://realpython.com/videos/why-oop-python/
Class Creation 09:43 https://realpython.com/lessons/python-class-creation/
Attributes 06:44 https://realpython.com/lessons/python-attributes/
Properties and Descriptors 07:51 https://realpython.com/lessons/python-properties-and-descriptors/
Class Methods 08:19 https://realpython.com/lessons/python-methods/
A Complete Example: Putting It All Together 10:22 https://realpython.com/lessons/python-class-object-review/
Class Concepts: Object-Oriented Programming in Python (Summary) 04:34 https://realpython.com/lessons/python-class-object-summary/
Privacy Policyhttps://realpython.com/privacy-policy/

Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover

Robots: max-image-preview:large


URLs of crawlers that visited me.