René's URL Explorer Experiment


Title: Object Inheritance in Python (Video) – Real Python

Open Graph Title: Object Inheritance in Python – Real Python

Description: Welcome to Inheritance and super() in Python. My name is Chris and I will be your guide. This course is split up into three lessons. The first lesson talks about inheritance and objects and classes in Python, and quickly shows you how to use the…

Open Graph Description: Welcome to Inheritance and super() in Python. My name is Chris and I will be your guide. This course is split up into three lessons. The first lesson talks about inheritance and objects and classes in Python, and quickly shows you how to use the…

Opengraph URL: https://realpython.com/videos/object-inheritance-python/

X: @realpython

direct link

Domain: realpython.com


Hey, it has json ld scripts:
  {
    "@context": "https://schema.org",
    "@type": "VideoObject",
    "name": "Object Inheritance in Python",
    "description": "Welcome to Inheritance and super() in Python. My name is Chris and I will be your guide. This course is split up into three lessons. The first lesson talks about inheritance and objects and classes in Python, and quickly shows you how to use the…",
    "thumbnailUrl": ["https://files.realpython.com/media/The-Super-Function-in-Python_Watermarked.58312d5835f7.jpg"],
    "uploadDate": "2020-01-14T14:00:00+00:00",
    "duration": "PT7M21S",
    "embedUrl": "https://player.vimeo.com/video/383618257",
    "potentialAction": {
      "@type": "SeekToAction",
      "target": "https://realpython.com/videos/object-inheritance-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/The-Super-Function-in-Python_Watermarked.58312d5835f7.jpg
og:imagehttps://files.realpython.com/media/The-Super-Function-in-Python_Watermarked.58312d5835f7.jpg
twitter:creator@realpython
og:typevideo.episode

Links:

https://realpython.com/
Start Herehttps://realpython.com/start-here/
Learn Python https://realpython.com/courses/python-super/continue/
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/courses/python-super/continue/
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%2Fobject-inheritance-python%2F
https://realpython.com/courses/python-super/#team
Supercharge Your Classes With Python super()https://realpython.com/courses/python-super/
Christopher Trudeauhttps://realpython.com/courses/python-super/#team
Recommended Tutorialhttps://realpython.com/python-super/
Course Slides (PDF)https://realpython.com/courses/python-super/downloads/python-super-slides/
Ask a Questionhttps://realpython.com/courses/python-super/continue/#discussion
https://realpython.com/feedback/survey/course/python-super/liked/?from=lesson-title
https://realpython.com/feedback/survey/course/python-super/disliked/?from=lesson-title
Contentshttps://realpython.com/courses/python-super/continue/#description
Transcripthttps://realpython.com/courses/python-super/continue/#transcript
Discussion (8)https://realpython.com/courses/python-super/continue/#discussion
Course Slides (PDF)https://realpython.com/courses/python-super/downloads/python-super-slides/
00:00https://realpython.com/courses/python-super/continue/#t=0.48
Welcome to Inheritance and super() in Python.https://realpython.com/courses/python-super/continue/#t=0.48
My name is Chris and I will be your guide.https://realpython.com/courses/python-super/continue/#t=2.76
This course is split up into three lessons.https://realpython.com/courses/python-super/continue/#t=4.98
The first lesson talks about inheritance and objects and classes in Python, andhttps://realpython.com/courses/python-super/continue/#t=7.32
quickly shows you how to use the super() function to get at the parent attributeshttps://realpython.com/courses/python-super/continue/#t=11.82
and methods in inheritance. The second and third lessons talk about single andhttps://realpython.com/courses/python-super/continue/#t=15.96
multiple inheritance and how to use super() to get at those objects as well.https://realpython.com/courses/python-super/continue/#t=20.04
00:24https://realpython.com/courses/python-super/continue/#t=24.09
First off, a quick warning.https://realpython.com/courses/python-super/continue/#t=24.09
The syntax for inheritance has changed between Python 2 and Python 3.https://realpython.com/courses/python-super/continue/#t=25.62
All the examples I’ll be using in this course will be based on Python 3.https://realpython.com/courses/python-super/continue/#t=29.67
00:33https://realpython.com/courses/python-super/continue/#t=33.5
So, let’s get started. In this lesson,https://realpython.com/courses/python-super/continue/#t=33.5
I’ll be talking about classes and objects, when to use them, why, and how. So,https://realpython.com/courses/python-super/continue/#t=35.66
what is an object? An object is just a way of grouping data and methods together,https://realpython.com/courses/python-super/continue/#t=40.07
usually for things that belong together and help you organize your code.https://realpython.com/courses/python-super/continue/#t=44.27
00:48https://realpython.com/courses/python-super/continue/#t=48.32
Frequently, an object maps to something in the real world.https://realpython.com/courses/python-super/continue/#t=48.32
If you’re writing software that describes a class schedule,https://realpython.com/courses/python-super/continue/#t=52.04
you might have a person object with a name and an address attribute, and methods onhttps://realpython.com/courses/python-super/continue/#t=55.7
that for adding that person to a course or saving their data. Similarly,https://realpython.com/courses/python-super/continue/#t=60.02
if you were writing accounting software,https://realpython.com/courses/python-super/continue/#t=64.37
your balance sheet object might have a list of assets and a list of liabilitieshttps://realpython.com/courses/python-super/continue/#t=66.44
as the attributes, and a method for totaling the assets or taking a report. A classhttps://realpython.com/courses/python-super/continue/#t=70.43
defines how to make an object,https://realpython.com/courses/python-super/continue/#t=76.97
and is actually in Python how you create the object itself,https://realpython.com/courses/python-super/continue/#t=79.25
which is called instantiation.https://realpython.com/courses/python-super/continue/#t=82.49
01:24https://realpython.com/courses/python-super/continue/#t=84.59
I’m going to start with some examples that are based on geometric shapes.https://realpython.com/courses/python-super/continue/#t=84.59
If you’re coding along with me, I’m putting it inside of a file called shapes.py.https://realpython.com/courses/python-super/continue/#t=88.25
01:32https://realpython.com/courses/python-super/continue/#t=92.4
This is the simplest possible class. It’s two lines.https://realpython.com/courses/python-super/continue/#t=92.4
It starts with the keyword class and the name of the class, Square. By conventionhttps://realpython.com/courses/python-super/continue/#t=96.08
in Python, you always capitalize the name of your class.https://realpython.com/courses/python-super/continue/#t=101.39
01:45https://realpython.com/courses/python-super/continue/#t=105.56
The pass keyword tells Python that there’s nothing else in this classhttps://realpython.com/courses/python-super/continue/#t=105.56
besides an empty definition.https://realpython.com/courses/python-super/continue/#t=108.98
If you open up a REPL and import the Square class from shapes,https://realpython.com/courses/python-super/continue/#t=111.38
you can then instantiate it by calling it. The Square,https://realpython.com/courses/python-super/continue/#t=115.82
on the right-hand side, capital S, is my class.https://realpython.com/courses/python-super/continue/#t=119.63
02:02https://realpython.com/courses/python-super/continue/#t=122.81
I call it using the parentheses (), and I end up with an object that I’ve namedhttps://realpython.com/courses/python-super/continue/#t=122.81
square. I can now add attributes to that square,https://realpython.com/courses/python-super/continue/#t=127.14
maybe a .length of 3,https://realpython.com/courses/python-super/continue/#t=131.18
and I can examine that square by using the built-in function called dir().https://realpython.com/courses/python-super/continue/#t=133.61
02:18https://realpython.com/courses/python-super/continue/#t=138.53
There’s a lot of content here. Don’t worry about it too much.https://realpython.com/courses/python-super/continue/#t=138.53
There’s just two things I want to point out. On the bottom right-hand side,https://realpython.com/courses/python-super/continue/#t=142.01
you’ll notice 'length'.https://realpython.com/courses/python-super/continue/#t=145.79
02:27https://realpython.com/courses/python-super/continue/#t=147.11
When I added the .length attribute to square, dir() shows that it’s there. In the tophttps://realpython.com/courses/python-super/continue/#t=147.11
left, there’s a special methodhttps://realpython.com/courses/python-super/continue/#t=152.33
'__class__' that contains information about the classhttps://realpython.com/courses/python-super/continue/#t=154.52
the object came from. If I look at that now in the REPL,https://realpython.com/courses/python-super/continue/#t=158.06
02:42https://realpython.com/courses/python-super/continue/#t=162.71
it produces a class object that says this is a Square.https://realpython.com/courses/python-super/continue/#t=162.71
You can always figure out what class an object comes from by examining thishttps://realpython.com/courses/python-super/continue/#t=167.12
property. Now I want to add some complexity to the Square.https://realpython.com/courses/python-super/continue/#t=171.71
02:56https://realpython.com/courses/python-super/continue/#t=176.15
First off, you’ll recall I added an attribute .length. Well,https://realpython.com/courses/python-super/continue/#t=176.15
the Square without a .length isn’t useful,https://realpython.com/courses/python-super/continue/#t=180.58
so I want to make sure that a .length is always there.https://realpython.com/courses/python-super/continue/#t=183.01
03:06https://realpython.com/courses/python-super/continue/#t=186.04
You can do this by changing the constructor.https://realpython.com/courses/python-super/continue/#t=186.04
The constructor in Python is called .__init__().https://realpython.com/courses/python-super/continue/#t=189.1
This method gets called every time a class constructs an object.https://realpython.com/courses/python-super/continue/#t=192.61
03:18https://realpython.com/courses/python-super/continue/#t=198.34
The default one doesn’t take any parameters.https://realpython.com/courses/python-super/continue/#t=198.34
I’ve changed this one to take a parameter called .length.https://realpython.com/courses/python-super/continue/#t=200.92
I then store that on the object itself that gets created from the class.https://realpython.com/courses/python-super/continue/#t=204.16
03:29https://realpython.com/courses/python-super/continue/#t=209.56
Now it won’t be possible to create a Square without a length being definedhttps://realpython.com/courses/python-super/continue/#t=209.56
and that attribute stored on the object. In addition to storing the .length,https://realpython.com/courses/python-super/continue/#t=214.09
I’ve also defined two methods, .area() and .perimeter().https://realpython.com/courses/python-super/continue/#t=219.25
03:43https://realpython.com/courses/python-super/continue/#t=223.0
These return the area and perimeter of the Square itself,https://realpython.com/courses/python-super/continue/#t=223.0
by doing math on the .length.https://realpython.com/courses/python-super/continue/#t=227.56
03:51https://realpython.com/courses/python-super/continue/#t=231.13
You can see this inside of the REPL. Importing from Squarehttps://realpython.com/courses/python-super/continue/#t=231.13
like before, now I’m passing in the length of 3https://realpython.com/courses/python-super/continue/#t=234.46
as part of the constructor. I get my .length,https://realpython.com/courses/python-super/continue/#t=238.15
it returns a value 3. I can call the .area() method,https://realpython.com/courses/python-super/continue/#t=241.96
I get 3 * 3 is 9. And once again,https://realpython.com/courses/python-super/continue/#t=245.86
I can look at dir() to examine the object.https://realpython.com/courses/python-super/continue/#t=249.43
04:13https://realpython.com/courses/python-super/continue/#t=253.69
In addition to having the '__class__' as before, on the bottom right sidehttps://realpython.com/courses/python-super/continue/#t=253.69
now you can see 'area', 'length', and 'perimeter', the methods and attributes that werehttps://realpython.com/courses/python-super/continue/#t=257.56
defined in the class. Great,https://realpython.com/courses/python-super/continue/#t=263.59
so I’ve got my square. Now I want to add another shape. This time,https://realpython.com/courses/python-super/continue/#t=268.33
it’s a Rectangle. This is going to be very similar to my Square.https://realpython.com/courses/python-super/continue/#t=272.02
04:36https://realpython.com/courses/python-super/continue/#t=276.07
Instead of needing a length on its own,https://realpython.com/courses/python-super/continue/#t=276.07
I need a length and a width. Like before, I used the .__init__()https://realpython.com/courses/python-super/continue/#t=278.77
method to pass in those as parameters and save them on the object.https://realpython.com/courses/python-super/continue/#t=283.33
04:47https://realpython.com/courses/python-super/continue/#t=287.29
And also like before, I have .area() and .perimeter() methods to do the calculationshttps://realpython.com/courses/python-super/continue/#t=287.29
based on the .length and .width of the Rectangle.https://realpython.com/courses/python-super/continue/#t=292.24
04:57https://realpython.com/courses/python-super/continue/#t=297.69
Once again, import it from the REPL,https://realpython.com/courses/python-super/continue/#t=297.69
05:01https://realpython.com/courses/python-super/continue/#t=301.38
passing in a length and a width.https://realpython.com/courses/python-super/continue/#t=301.38
05:04https://realpython.com/courses/python-super/continue/#t=304.59
And I get the expected result.https://realpython.com/courses/python-super/continue/#t=304.59
05:08https://realpython.com/courses/python-super/continue/#t=308.25
The two classes I’ve defined so far, the Square and the Rectangle, are very,https://realpython.com/courses/python-super/continue/#t=308.25
very similar. This is a problem.https://realpython.com/courses/python-super/continue/#t=311.37
If it turns out there was a bug in the .area() method of one,https://realpython.com/courses/python-super/continue/#t=313.86
because I duplicated it in the other, I probably have to fix it in two places.https://realpython.com/courses/python-super/continue/#t=317.19
05:21https://realpython.com/courses/python-super/continue/#t=321.36
One of the ways of simplifying this is by using inheritance. Instead of defininghttps://realpython.com/courses/python-super/continue/#t=321.36
the Square and the Rectangle separately,https://realpython.com/courses/python-super/continue/#t=325.77
I’m going to define the Square based on the Rectangle, replacing the codehttps://realpython.com/courses/python-super/continue/#t=328.02
you see here with the new code at the bottom.https://realpython.com/courses/python-super/continue/#t=332.1
05:35https://realpython.com/courses/python-super/continue/#t=335.67
In this case, Square, having Rectangle in brackets,https://realpython.com/courses/python-super/continue/#t=335.67
inherits from the Rectangle object.https://realpython.com/courses/python-super/continue/#t=339.42
I don’t have to define anything new on it.https://realpython.com/courses/python-super/continue/#t=342.45
05:45https://realpython.com/courses/python-super/continue/#t=345.0
It will inherit anything that I don’t override from the Rectangle.https://realpython.com/courses/python-super/continue/#t=345.0
The .__init__() method does need to be overridden.https://realpython.com/courses/python-super/continue/#t=350.07
The Rectangle takes a length and a width. The Square only takes a length,https://realpython.com/courses/python-super/continue/#t=352.68
so I need to redefine this method. This is where super() comes in.https://realpython.com/courses/python-super/continue/#t=356.82
06:01https://realpython.com/courses/python-super/continue/#t=361.22
super() allows me to access the parent’s methods—in this case,https://realpython.com/courses/python-super/continue/#t=361.22
the parent Rectangle.__init__() method—https://realpython.com/courses/python-super/continue/#t=365.63
and I’m going to pass in a length for the Rectangle length and width.https://realpython.com/courses/python-super/continue/#t=368.6
06:13https://realpython.com/courses/python-super/continue/#t=373.58
I don’t have to specify anything else.https://realpython.com/courses/python-super/continue/#t=373.58
The .area() and .perimeter() will now come from the Rectangle class from before.https://realpython.com/courses/python-super/continue/#t=375.92
06:22https://realpython.com/courses/python-super/continue/#t=382.7
Inside of shapes.py,https://realpython.com/courses/python-super/continue/#t=382.7
I’ve replaced the old Square definition with this new inherited code.https://realpython.com/courses/python-super/continue/#t=383.99
Like before, I can use the REPL and import the Square,https://realpython.com/courses/python-super/continue/#t=388.04
instantiate it with a length of 3. If I look at the .__class__,https://realpython.com/courses/python-super/continue/#t=392.15
not surprisingly, it’s a Square. If I look at the dir() of the object,https://realpython.com/courses/python-super/continue/#t=396.38
you’ll see a few new things. For starters, at the bottom,https://realpython.com/courses/python-super/continue/#t=401.57
the 'width', 'perimeter', 'length', and 'area' are all there.https://realpython.com/courses/python-super/continue/#t=405.56
06:49https://realpython.com/courses/python-super/continue/#t=409.25
The .area(), .perimeter(), and .width are inherited from the Rectangle class.https://realpython.com/courses/python-super/continue/#t=409.25
The .length is passed in, in the constructor.https://realpython.com/courses/python-super/continue/#t=413.66
06:57https://realpython.com/courses/python-super/continue/#t=417.41
I can also look at the .__bases__ attribute of the .__class__ attribute.https://realpython.com/courses/python-super/continue/#t=417.41
This can tell you where your inheritance is from.https://realpython.com/courses/python-super/continue/#t=422.12
Because Square inherits from Rectangle,https://realpython.com/courses/python-super/continue/#t=425.96
the .__bases__ value shows the Rectangle class. In the next lesson,https://realpython.com/courses/python-super/continue/#t=428.15
I’m going to show you how to use super()https://realpython.com/courses/python-super/continue/#t=433.61
to look at different methods and attributes inside of single inheritance.https://realpython.com/courses/python-super/continue/#t=435.29
April 12, 2020https://realpython.com/courses/python-super/continue/#comment-02175858-6711-4213-86c0-c30d4eab1644
May 4, 2020https://realpython.com/courses/python-super/continue/#comment-e841d779-c582-40cb-bc10-121eeaaac829
Aug. 11, 2020https://realpython.com/courses/python-super/continue/#comment-c63e0818-a88f-4e05-837d-3ff612147643
Aug. 12, 2020https://realpython.com/courses/python-super/continue/#comment-48ebefce-fc75-4c1e-ae98-d39c83fb8bc6
Aug. 12, 2020https://realpython.com/courses/python-super/continue/#comment-d1d5098e-5a9d-4a33-a6ea-00803eb51e50
github.com/cltrudeau/purdyhttps://github.com/cltrudeau/purdy
Nov. 13, 2020https://realpython.com/courses/python-super/continue/#comment-06a866ac-5c06-4ea0-8f94-ae836fba9d1a
Nov. 13, 2020https://realpython.com/courses/python-super/continue/#comment-2461f176-5ca0-40eb-8139-7df64cdd7c6c
realpython.com/courses/python-modules-packages/https://realpython.com/courses/python-modules-packages/
Nov. 13, 2020https://realpython.com/courses/python-super/continue/#comment-f0d26e56-157e-4c57-856c-9f3a697cbd45
Become a Memberhttps://realpython.com/account/join/
Overviewhttps://realpython.com/courses/python-super/
https://realpython.com/lessons/super-and-inheritance-hierarchy/
Object Inheritance in Python 07:21 https://realpython.com/videos/object-inheritance-python/
super() and the Inheritance Hierarchy 05:37 https://realpython.com/lessons/super-and-inheritance-hierarchy/
Multiple Inheritance in Python 09:55 https://realpython.com/lessons/multiple-inheritance-python/
Supercharge Your Classes With Python super() (Quiz) 03:30 https://realpython.com/lessons/super-classes-quiz/
Supercharge Your Classes With Python super() (Summary) 00:38 https://realpython.com/lessons/super-classes-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.