René's URL Explorer Experiment


Title: Inheritance in Python (Video) – Real Python

Open Graph Title: Inheritance in Python – Real Python

Description: You’re almost always using some form of inheritance within Python, even if you don’t explicitly declare it. To demonstrate that, I’m going to use the Python interactive shell. I’m going to start by creating a new class called MyClass—a very creative…

Open Graph Description: You’re almost always using some form of inheritance within Python, even if you don’t explicitly declare it. To demonstrate that, I’m going to use the Python interactive shell. I’m going to start by creating a new class called MyClass—a very creative…

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

X: @realpython

direct link

Domain: realpython.com


Hey, it has json ld scripts:
  {
    "@context": "https://schema.org",
    "@type": "VideoObject",
    "name": "Inheritance in Python",
    "description": "You’re almost always using some form of inheritance within Python, even if you don’t explicitly declare it. To demonstrate that, I’m going to use the Python interactive shell. I’m going to start by creating a new class called MyClass—a very creative…",
    "thumbnailUrl": ["https://files.realpython.com/media/Inheritance-vs-Composition-in-Python_Watermarked_1.aec726368a97.jpg"],
    "uploadDate": "2020-04-14T14:00:00+00:00",
    "duration": "PT7M13S",
    "embedUrl": "https://player.vimeo.com/video/404796089",
    "potentialAction": {
      "@type": "SeekToAction",
      "target": "https://realpython.com/videos/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/Inheritance-vs-Composition-in-Python_Watermarked_1.aec726368a97.jpg
og:imagehttps://files.realpython.com/media/Inheritance-vs-Composition-in-Python_Watermarked_1.aec726368a97.jpg
twitter:creator@realpython
og:typevideo.episode

Links:

https://realpython.com/
Start Herehttps://realpython.com/start-here/
Learn Python https://realpython.com/videos/inheritance-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/inheritance-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%2Finheritance-python%2F
https://realpython.com/courses/inheritance-composition-python/#team
Inheritance and Composition: A Python OOP Guidehttps://realpython.com/courses/inheritance-composition-python/
Austin Cepaliahttps://realpython.com/courses/inheritance-composition-python/#team
Recommended Tutorialhttps://realpython.com/inheritance-composition-python/
Sample Code (.zip)https://realpython.com/courses/inheritance-composition-python/downloads/oop-inheritance-composition-code/
Ask a Questionhttps://realpython.com/videos/inheritance-python/#discussion
https://realpython.com/feedback/survey/course/inheritance-composition-python/liked/?from=lesson-title
https://realpython.com/feedback/survey/course/inheritance-composition-python/disliked/?from=lesson-title
Transcripthttps://realpython.com/videos/inheritance-python/#transcript
Discussion (8)https://realpython.com/videos/inheritance-python/#discussion
00:00https://realpython.com/videos/inheritance-python/#t=0.51
You’re almost always using some form of inheritance within Python,https://realpython.com/videos/inheritance-python/#t=0.51
even if you don’t explicitly declare it. To demonstrate that, I’m going to use thehttps://realpython.com/videos/inheritance-python/#t=5.28
Python interactive shell. I’m going to start by creating a new classhttps://realpython.com/videos/inheritance-python/#t=10.47
called MyClass—a very creative name.https://realpython.com/videos/inheritance-python/#t=15.3
00:19https://realpython.com/videos/inheritance-python/#t=19.11
I want to leave this class blank, and so I’ll just write pass here.https://realpython.com/videos/inheritance-python/#t=19.11
pass is a special keyword that will allow you to create a function, class, or loophttps://realpython.com/videos/inheritance-python/#t=24.72
that is effectively empty.https://realpython.com/videos/inheritance-python/#t=31.02
00:33https://realpython.com/videos/inheritance-python/#t=33.42
Now, I will instantiate this class into a new object called c. Python has a built-inhttps://realpython.com/videos/inheritance-python/#t=33.42
function called dir(),https://realpython.com/videos/inheritance-python/#t=41.13
which returns a list of all the members of the classhttps://realpython.com/videos/inheritance-python/#t=42.9
you pass in. A class’s members are just the attributes and methods that make uphttps://realpython.com/videos/inheritance-python/#t=46.59
the class,https://realpython.com/videos/inheritance-python/#t=52.65
including the special .__init__() method that’s used for object instantiation.https://realpython.com/videos/inheritance-python/#t=53.67
00:59https://realpython.com/videos/inheritance-python/#t=59.07
But we didn’t declare any attributes or methods,https://realpython.com/videos/inheritance-python/#t=59.07
so this class has no members, right? Let’s find out.https://realpython.com/videos/inheritance-python/#t=62.28
01:08https://realpython.com/videos/inheritance-python/#t=68.31
That’s probably not what you were expecting.https://realpython.com/videos/inheritance-python/#t=68.31
What are all of these members with double underscores (__), and where are they cominghttps://realpython.com/videos/inheritance-python/#t=71.73
from?https://realpython.com/videos/inheritance-python/#t=76.05
These members are actually all methods called magic methods, or dunder methods,https://realpython.com/videos/inheritance-python/#t=77.16
01:23https://realpython.com/videos/inheritance-python/#t=83.49
named after their double underscores.https://realpython.com/videos/inheritance-python/#t=83.49
Some of these may look familiar to you, such as __init__,https://realpython.com/videos/inheritance-python/#t=86.43
which we use as the constructor for constructing new classes.https://realpython.com/videos/inheritance-python/#t=90.51
01:35https://realpython.com/videos/inheritance-python/#t=95.13
We also used __init__ to declare instance attributes.https://realpython.com/videos/inheritance-python/#t=95.13
To explain where these are coming from,https://realpython.com/videos/inheritance-python/#t=99.6
let me create one more object and list its members.https://realpython.com/videos/inheritance-python/#t=101.85
01:49https://realpython.com/videos/inheritance-python/#t=109.76
And would you look at that?https://realpython.com/videos/inheritance-python/#t=109.76
It looks like the o object and the c object have very similar members, except thehttps://realpython.com/videos/inheritance-python/#t=111.26
c object—https://realpython.com/videos/inheritance-python/#t=117.32
which came from our custom class—has some extra members, like __weakref__.https://realpython.com/videos/inheritance-python/#t=118.34
02:04https://realpython.com/videos/inheritance-python/#t=124.04
You might be able to guess what’s going on here, but let me explain. This ohttps://realpython.com/videos/inheritance-python/#t=124.04
object was instantiated from the object class,https://realpython.com/videos/inheritance-python/#t=128.669
which is super confusing considering we’ve been calling every instance of anyhttps://realpython.com/videos/inheritance-python/#t=133.04
class an object. Well,https://realpython.com/videos/inheritance-python/#t=137.75
this object class is not a class that we define ourselves.https://realpython.com/videos/inheritance-python/#t=140.33
02:24https://realpython.com/videos/inheritance-python/#t=144.89
It’s built into Python and it’s this object class that defines these strangehttps://realpython.com/videos/inheritance-python/#t=144.89
members that we’re seeing.https://realpython.com/videos/inheritance-python/#t=150.56
And, as it turns out, every custom class that we create automatically inheritshttps://realpython.com/videos/inheritance-python/#t=152.51
from this object class behind the scenes,https://realpython.com/videos/inheritance-python/#t=158.81
even if we don’t explicitly make it.https://realpython.com/videos/inheritance-python/#t=162.23
02:45https://realpython.com/videos/inheritance-python/#t=165.26
That explains why the c object has all of these members too—ithttps://realpython.com/videos/inheritance-python/#t=165.26
inherited it from the object class.https://realpython.com/videos/inheritance-python/#t=170.36
The extra members present in our custom class came from elsewhere,https://realpython.com/videos/inheritance-python/#t=173.51
but that’s beyond the scope of this course.https://realpython.com/videos/inheritance-python/#t=177.77
03:00https://realpython.com/videos/inheritance-python/#t=180.55
The point is every class in Python implicitly inherits from this objecthttps://realpython.com/videos/inheritance-python/#t=180.55
superclass,https://realpython.com/videos/inheritance-python/#t=185.68
which defines all these magic methods that Python uses to manage our objects.https://realpython.com/videos/inheritance-python/#t=186.79
03:14https://realpython.com/videos/inheritance-python/#t=194.08
Another place where inheritance is used is with exceptions.https://realpython.com/videos/inheritance-python/#t=194.08
Exceptionhttps://realpython.com/videos/inheritance-python/#t=199.0
objects are special in that they can be thrown up a call stack and caughthttps://realpython.com/videos/inheritance-python/#t=199.78
anywhere along that stack.https://realpython.com/videos/inheritance-python/#t=205.36
03:27https://realpython.com/videos/inheritance-python/#t=207.85
If no code catches that exception object before it reaches the top,https://realpython.com/videos/inheritance-python/#t=207.85
the program crashes and you’re left with a stack trace.https://realpython.com/videos/inheritance-python/#t=212.59
If you’re interested in learning more about exceptions,https://realpython.com/videos/inheritance-python/#t=217.06
we have a course for that, which I will link down in the video notes below.https://realpython.com/videos/inheritance-python/#t=220.27
03:45https://realpython.com/videos/inheritance-python/#t=225.13
Let’s see how they work with inheritance.https://realpython.com/videos/inheritance-python/#t=225.13
Someone new to exceptions might try to create their own exception class as if ithttps://realpython.com/videos/inheritance-python/#t=228.46
were a normal class, just like this.https://realpython.com/videos/inheritance-python/#t=233.5
03:59https://realpython.com/videos/inheritance-python/#t=239.41
Exceptions can be raised with the raise keyword.https://realpython.com/videos/inheritance-python/#t=239.41
This is similar to throw in other languages. All right,https://realpython.com/videos/inheritance-python/#t=243.19
we got a stack trace—but it’s not from our exception.https://realpython.com/videos/inheritance-python/#t=248.2
04:12https://realpython.com/videos/inheritance-python/#t=252.7
There’s a TypeError—not MyError.https://realpython.com/videos/inheritance-python/#t=252.7
Python has identified that we’re trying to create a custom exception class,https://realpython.com/videos/inheritance-python/#t=256.24
but we didn’t do it right.https://realpython.com/videos/inheritance-python/#t=260.92
04:23https://realpython.com/videos/inheritance-python/#t=263.02
It’s saying that our class must inherit from BaseException.https://realpython.com/videos/inheritance-python/#t=263.02
BaseException is a built-in class provided for all error types.https://realpython.com/videos/inheritance-python/#t=267.52
There are lots of classes that derive from BaseException, such ashttps://realpython.com/videos/inheritance-python/#t=273.91
SyntaxError and ArithmeticError,https://realpython.com/videos/inheritance-python/#t=278.55
which means that we can use them in place of BaseException.https://realpython.com/videos/inheritance-python/#t=282.22
04:47https://realpython.com/videos/inheritance-python/#t=287.44
Remember, if SyntaxError inherits from BaseException,https://realpython.com/videos/inheritance-python/#t=287.44
it means SyntaxError is a BaseException because it inherits all of the members ofhttps://realpython.com/videos/inheritance-python/#t=292.15
BaseException. What specific exception type you inherit fromhttps://realpython.com/videos/inheritance-python/#t=298.93
depends on the situation at hand. BaseException is not supposed to be inheritedhttps://realpython.com/videos/inheritance-python/#t=304.0
from directly, so we’ll choose its next child,https://realpython.com/videos/inheritance-python/#t=309.79
a class called Exception.https://realpython.com/videos/inheritance-python/#t=313.48
05:16https://realpython.com/videos/inheritance-python/#t=316.0
This is the class that the official documentation recommends inheriting from.https://realpython.com/videos/inheritance-python/#t=316.0
I’ll redefine the MyError class,https://realpython.com/videos/inheritance-python/#t=321.73
but this time I’ll put the name of the class I want to inherit from inhttps://realpython.com/videos/inheritance-python/#t=324.46
parentheses, after the class name.https://realpython.com/videos/inheritance-python/#t=329.17
05:33https://realpython.com/videos/inheritance-python/#t=333.37
MyError is now an Exception,https://realpython.com/videos/inheritance-python/#t=333.37
literally. I could write pass in here and leave the custom exception type blank,https://realpython.com/videos/inheritance-python/#t=336.28
05:42https://realpython.com/videos/inheritance-python/#t=342.4
but I want to do one more thing.https://realpython.com/videos/inheritance-python/#t=342.4
Exceptions usually have some sort of message that’s intended to tell the user orhttps://realpython.com/videos/inheritance-python/#t=345.37
developer what went wrong.https://realpython.com/videos/inheritance-python/#t=350.89
The Exception class contains an attribute for the message,https://realpython.com/videos/inheritance-python/#t=353.41
05:57https://realpython.com/videos/inheritance-python/#t=357.89
and as such, it’s been passed down to ourhttps://realpython.com/videos/inheritance-python/#t=357.89
MyError class. To use it,https://realpython.com/videos/inheritance-python/#t=360.95
we first must add an .__init__() method to this class.https://realpython.com/videos/inheritance-python/#t=363.92
This exception will need to be passed a message when it is raised.https://realpython.com/videos/inheritance-python/#t=368.03
06:15https://realpython.com/videos/inheritance-python/#t=375.08
Now, we can use the super() function to call the .__init__() method of Exception,https://realpython.com/videos/inheritance-python/#t=375.08
06:22https://realpython.com/videos/inheritance-python/#t=382.04
passing in the message. In essence, when we create ahttps://realpython.com/videos/inheritance-python/#t=382.04
MyError object,https://realpython.com/videos/inheritance-python/#t=386.57
we’re asking for a message and then passing it to the parent.https://realpython.com/videos/inheritance-python/#t=388.76
06:33https://realpython.com/videos/inheritance-python/#t=393.68
Finally, now that we’re done writing our class, we can raise MyError,https://realpython.com/videos/inheritance-python/#t=393.68
passing in whatever message we want.https://realpython.com/videos/inheritance-python/#t=398.69
I’ll choose a particularly unhelpful one that you might’ve seen before.https://realpython.com/videos/inheritance-python/#t=401.51
06:47https://realpython.com/videos/inheritance-python/#t=407.21
There’s our custom exception with the message.https://realpython.com/videos/inheritance-python/#t=407.21
This was possible due to inheritance.https://realpython.com/videos/inheritance-python/#t=410.27
We created a custom class that inherited from Exception,https://realpython.com/videos/inheritance-python/#t=413.57
so Python recognized our class as an exception.https://realpython.com/videos/inheritance-python/#t=417.71
07:02https://realpython.com/videos/inheritance-python/#t=422.18
We even utilized the inherited .message attribute to allow our custom exceptionhttps://realpython.com/videos/inheritance-python/#t=422.18
class to be utilized with a custom message.https://realpython.com/videos/inheritance-python/#t=428.18
June 26, 2020https://realpython.com/videos/inheritance-python/#comment-694bc892-f063-4b06-960e-9ecde5490f44
June 26, 2020https://realpython.com/videos/inheritance-python/#comment-63abba02-9081-4173-80d6-db7e74bc60f2
realpython.com/courses/introduction-python-exceptions/https://realpython.com/courses/introduction-python-exceptions/
July 27, 2020https://realpython.com/videos/inheritance-python/#comment-eeb8b1e0-9690-4b3c-8b70-69ba678ac0ea
July 27, 2020https://realpython.com/videos/inheritance-python/#comment-79693d9c-75a3-4f48-890a-f27bb3a73869
realpython.com/team/acepalia/https://realpython.com/team/acepalia/
Jan. 13, 2022https://realpython.com/videos/inheritance-python/#comment-d71f5038-56d4-44ed-900b-4ff3f3f1cfc4
Jan. 13, 2022https://realpython.com/videos/inheritance-python/#comment-af894661-eee2-4033-a021-d405a2d60a57
April 25, 2023https://realpython.com/videos/inheritance-python/#comment-d52dc669-a101-4684-b0e1-8f96c7a6835a
April 26, 2023https://realpython.com/videos/inheritance-python/#comment-2d99f26c-9105-4841-a6e7-40c363ea98cc
Become a Memberhttps://realpython.com/account/join/
https://realpython.com/videos/composition/
Overviewhttps://realpython.com/courses/inheritance-composition-python/
https://realpython.com/videos/uml-diagrams/
Inheritance and Composition: A Python OOP Guide (Overview) 02:45 https://realpython.com/videos/inheritance-composition-python-overview/
What Are Inheritance and Composition? 02:17 https://realpython.com/videos/what-are-inheritance-and-composition/
Inheritance 08:20 https://realpython.com/videos/inheritance/
Composition 04:41 https://realpython.com/videos/composition/
Inheritance in Python 07:13 https://realpython.com/videos/inheritance-python/
UML Diagrams 03:39 https://realpython.com/videos/uml-diagrams/
Interfaces 11:13 https://realpython.com/lessons/interfaces/
Implementing a Class Hierarchy 14:13 https://realpython.com/lessons/implementing-class-hierarchy/
Abstract Classes 05:12 https://realpython.com/lessons/abstract-classes/
Implementing the Productivity System 07:05 https://realpython.com/lessons/implementing-productivity-system/
Multiple Inheritance 11:15 https://realpython.com/lessons/multiple-inheritance/
C3 Superclass Linearization (Optional) 07:55 https://realpython.com/lessons/c3-superclass-linearization-optional/
Avoiding the Diamond Problem 10:32 https://realpython.com/lessons/avoiding-diamond-problem/
Utilizing Composition 10:53 https://realpython.com/lessons/utilizing-composition/
Flexible Designs With Composition 15:43 https://realpython.com/lessons/flexible-designs-composition/
Modifying Object Behavior With Composition 01:56 https://realpython.com/lessons/modifying-object-behavior-composition/
Inheritance Best Practices 07:25 https://realpython.com/lessons/inheritance-best-practices/
Mixin Classes 08:50 https://realpython.com/lessons/mixin-classes/
Further Improving Design With Composition 15:37 https://realpython.com/lessons/further-improving-design-composition/
Composition to Change Runtime Behavior 05:20 https://realpython.com/lessons/composition-change-runtime-behavior/
Deciding Between Composition and Inheritance 03:31 https://realpython.com/lessons/deciding-between-composition-and-inheritance/
Inheritance and Composition: A Python OOP Guide (Quiz) 04:30 https://realpython.com/lessons/inheritance-and-composition-a-python-oop-guide-quiz/
Inheritance and Composition: A Python OOP Guide (Summary) 01:15 https://realpython.com/lessons/inheritance-and-composition-python-oop-guide-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.