|
| https://realpython.com/ |
| Start Here | https://realpython.com/start-here/ |
|
Learn Python
| https://realpython.com/lessons/python-class-standard-library/ |
| Python Tutorials →In-depth articles and video courses | https://realpython.com/search?kind=article&kind=course&order=newest |
| Learning Paths →Guided study plans for accelerated learning | https://realpython.com/learning-paths/ |
| Quizzes & Exercises →Check your learning progress | https://realpython.com/quizzes/ |
| Browse Topics →Focus on a specific area or skill level | https://realpython.com/tutorials/all/ |
| Community Chat →Learn with other Pythonistas | https://realpython.com/community/ |
| Office Hours →Live Q&A calls with Python experts | https://realpython.com/office-hours/ |
| Podcast →Hear what’s new in the world of Python | https://realpython.com/podcasts/rpp/ |
| Books →Round out your knowledge and learn offline | https://realpython.com/products/books/ |
| Reference →Concise definitions for common Python terms | https://realpython.com/ref/ |
| Code Mentor →BetaPersonalized code assistance & learning tools | https://realpython.com/mentor/ |
| Unlock All Content → | https://realpython.com/account/join/ |
|
More
| https://realpython.com/lessons/python-class-standard-library/ |
| Learner Stories | https://realpython.com/learner-stories/ |
| Python Newsletter | https://realpython.com/newsletter/ |
| Python Job Board | https://www.pythonjobshq.com |
| Meet the Team | https://realpython.com/team/ |
| Become a Tutorial Writer | https://realpython.com/write-for-us/ |
| Become a Video Instructor | https://realpython.com/become-an-instructor/ |
| Search | https://realpython.com/search |
| https://realpython.com/search |
| Join | https://realpython.com/account/join/ |
| Sign‑In | https://realpython.com/account/login/?next=%2Flessons%2Fpython-class-standard-library%2F |
| Unlock This Lesson | https://realpython.com/account/join/?utm_source=rp_lesson&utm_content=python-class-inheritance |
| Unlock This Lesson | https://realpython.com/account/join/?utm_source=rp_lesson&utm_content=python-class-inheritance |
| https://realpython.com/courses/python-class-inheritance/#team |
| Inheritance and Internals: Object-Oriented Programming in Python | https://realpython.com/courses/python-class-inheritance/ |
| Christopher Trudeau | https://realpython.com/courses/python-class-inheritance/#team |
| Recommended Tutorial | https://realpython.com/python-classes/ |
| Course Slides (.pdf) | https://realpython.com/courses/python-class-inheritance/downloads/python-class-inheritance-slides/ |
| Sample Code (.zip) | https://realpython.com/courses/python-class-inheritance/downloads/python-class-inheritance-code/ |
| Ask a Question | https://realpython.com/lessons/python-class-standard-library/#discussion |
| https://realpython.com/feedback/survey/course/python-class-inheritance/liked/?from=lesson-title |
| https://realpython.com/feedback/survey/course/python-class-inheritance/disliked/?from=lesson-title |
| Transcript | https://realpython.com/lessons/python-class-standard-library/#transcript |
| Discussion | https://realpython.com/lessons/python-class-standard-library/#discussion |
| 00:00 | https://realpython.com/lessons/python-class-standard-library/#t=0.53 |
| In the previous lesson, | https://realpython.com/lessons/python-class-standard-library/#t=0.53 |
| I showed you some more dunder methods and probably exhausted your eyes from all | https://realpython.com/lessons/python-class-standard-library/#t=1.63 |
| their rolling at my puns. In this lesson, | https://realpython.com/lessons/python-class-standard-library/#t=5.75 |
| I’ll show you some of the classes available to you in the Python standard | https://realpython.com/lessons/python-class-standard-library/#t=8.74 |
| library. Data classes were introduced in Python 3.7, | https://realpython.com/lessons/python-class-standard-library/#t=11.99 |
| and they’re a shortcut for creating the attributes on a class. | https://realpython.com/lessons/python-class-standard-library/#t=17.22 |
| 00:21 | https://realpython.com/lessons/python-class-standard-library/#t=21.27 |
| They’re kind of like a dictionary and a namedtuple got together and had a | https://realpython.com/lessons/python-class-standard-library/#t=21.27 |
| wonderful little love child. | https://realpython.com/lessons/python-class-standard-library/#t=25.08 |
| You create a data class by wrapping a class declaration with the @dataclass | https://realpython.com/lessons/python-class-standard-library/#t=26.94 |
| decorator. There are other ways as well if you dig into the docs, | https://realpython.com/lessons/python-class-standard-library/#t=31.47 |
| but I’m going to stick with this one. | https://realpython.com/lessons/python-class-standard-library/#t=34.96 |
| 00:37 | https://realpython.com/lessons/python-class-standard-library/#t=37.39 |
| In addition to allowing you to specify the names of attributes, | https://realpython.com/lessons/python-class-standard-library/#t=37.39 |
| you can indicate the type of the attribute in case you’re using type annotation | https://realpython.com/lessons/python-class-standard-library/#t=40.71 |
| tools. On top of all this, it is still a class, | https://realpython.com/lessons/python-class-standard-library/#t=44.73 |
| so you can add your own methods. | https://realpython.com/lessons/python-class-standard-library/#t=48.99 |
| 00:51 | https://realpython.com/lessons/python-class-standard-library/#t=51.83 |
| Now that Python 3.6 is no longer supported, | https://realpython.com/lessons/python-class-standard-library/#t=51.83 |
| it is safer to write code that assumes data classes exist. Increasingly, | https://realpython.com/lessons/python-class-standard-library/#t=54.07 |
| I’m writing a lot of my classes as data classes, | https://realpython.com/lessons/python-class-standard-library/#t=59.31 |
| so I don’t have to write the .__init__() boilerplate. Let’s go look at some. | https://realpython.com/lessons/python-class-standard-library/#t=62.03 |
| 01:08 | https://realpython.com/lessons/python-class-standard-library/#t=68.31 |
| First off, I need to import the decorator. | https://realpython.com/lessons/python-class-standard-library/#t=68.31 |
| 01:14 | https://realpython.com/lessons/python-class-standard-library/#t=74.45 |
| Then I use it to wrap a class definition, | https://realpython.com/lessons/python-class-standard-library/#t=74.45 |
| 01:21 | https://realpython.com/lessons/python-class-standard-library/#t=81.51 |
| yet another version of Point, | https://realpython.com/lessons/python-class-standard-library/#t=81.51 |
| and inside the class, I indicate the name and type of any attribute I want | https://realpython.com/lessons/python-class-standard-library/#t=84.49 |
| on the class. | https://realpython.com/lessons/python-class-standard-library/#t=89.29 |
| 01:33 | https://realpython.com/lessons/python-class-standard-library/#t=93.43 |
| If you’re using a more recent version of Python, | https://realpython.com/lessons/python-class-standard-library/#t=93.43 |
| you can even use the or-ed notation on the type. | https://realpython.com/lessons/python-class-standard-library/#t=95.55 |
| What the data class does for you is automatically implement a bunch of stuff. For | https://realpython.com/lessons/python-class-standard-library/#t=99.08 |
| free, | https://realpython.com/lessons/python-class-standard-library/#t=104.05 |
| you get a .__init__() that uses the same arguments in the same order as | https://realpython.com/lessons/python-class-standard-library/#t=104.43 |
| they’re declared, x and y in this case. | https://realpython.com/lessons/python-class-standard-library/#t=108.61 |
| 01:52 | https://realpython.com/lessons/python-class-standard-library/#t=112.35 |
| You also get an implementation of .__str__() and .__repr__(), | https://realpython.com/lessons/python-class-standard-library/#t=112.35 |
| and as it’s a class, you instantiate it like any other. | https://realpython.com/lessons/python-class-standard-library/#t=117.01 |
| Creating my point, naming it p, with x 3 and y 4. | https://realpython.com/lessons/python-class-standard-library/#t=122.53 |
| 02:08 | https://realpython.com/lessons/python-class-standard-library/#t=128.2 |
| There’s .x, and there’s .y, just like any good attribute. | https://realpython.com/lessons/python-class-standard-library/#t=128.2 |
| The dataclass module also has a utility function. | https://realpython.com/lessons/python-class-standard-library/#t=133.23 |
| 02:20 | https://realpython.com/lessons/python-class-standard-library/#t=140.56 |
| Using it on a dataclass object, | https://realpython.com/lessons/python-class-standard-library/#t=140.56 |
| it automatically turns it into a tuple using the order of the named attributes. | https://realpython.com/lessons/python-class-standard-library/#t=144.12 |
| See what I mean about the dictionary and namedtuple getting a little romantic? | https://realpython.com/lessons/python-class-standard-library/#t=150.04 |
| 02:34 | https://realpython.com/lessons/python-class-standard-library/#t=154.88 |
| Let me create another point. | https://realpython.com/lessons/python-class-standard-library/#t=154.88 |
| The dunder comparison methods are also implemented on a data class. | https://realpython.com/lessons/python-class-standard-library/#t=158.3 |
| p and q aren’t the same. p and p are. | https://realpython.com/lessons/python-class-standard-library/#t=163.88 |
| 02:48 | https://realpython.com/lessons/python-class-standard-library/#t=168.68 |
| Let me add another point. | https://realpython.com/lessons/python-class-standard-library/#t=168.68 |
| 02:53 | https://realpython.com/lessons/python-class-standard-library/#t=173.34 |
| And notice, the equality check isn’t checking if they’re the same object, | https://realpython.com/lessons/python-class-standard-library/#t=173.34 |
| but comparing the attribute values. p and r are different objects, | https://realpython.com/lessons/python-class-standard-library/#t=177.86 |
| but show as equal because they have x = 3 and y = 4. | https://realpython.com/lessons/python-class-standard-library/#t=182.62 |
| 03:11 | https://realpython.com/lessons/python-class-standard-library/#t=191.18 |
| An enum is a grouping of constants. | https://realpython.com/lessons/python-class-standard-library/#t=191.18 |
| Note that the iterable utility enumerate() existed in the language | https://realpython.com/lessons/python-class-standard-library/#t=194.15 |
| before enums were added, so don’t confuse the two concepts. And yes, | https://realpython.com/lessons/python-class-standard-library/#t=198.86 |
| you can enumerate an enumeration. Isn’t coding fun? | https://realpython.com/lessons/python-class-standard-library/#t=203.86 |
| 03:28 | https://realpython.com/lessons/python-class-standard-library/#t=208.88 |
| The idea of an enum is built into some programming languages. | https://realpython.com/lessons/python-class-standard-library/#t=208.88 |
| Python added it in 3.4, but instead of creating a new keyword, | https://realpython.com/lessons/python-class-standard-library/#t=213.27 |
| they used class magic, some very black meta class magic in fact, | https://realpython.com/lessons/python-class-standard-library/#t=217.68 |
| but you don’t need to know that to use them. To write an enum, | https://realpython.com/lessons/python-class-standard-library/#t=222.95 |
| you inherit from the Enum class, and kind of like a data class, | https://realpython.com/lessons/python-class-standard-library/#t=227.0 |
| you declare the constants as class members. And it’s still a class, | https://realpython.com/lessons/python-class-standard-library/#t=230.52 |
| so you can add methods and do other classy things. | https://realpython.com/lessons/python-class-standard-library/#t=235.28 |
| 03:59 | https://realpython.com/lessons/python-class-standard-library/#t=239.36 |
| Let’s go build an enum. Importing the base class … | https://realpython.com/lessons/python-class-standard-library/#t=239.36 |
| 04:07 | https://realpython.com/lessons/python-class-standard-library/#t=247.45 |
| extending Enum … | https://realpython.com/lessons/python-class-standard-library/#t=247.45 |
| 04:13 | https://realpython.com/lessons/python-class-standard-library/#t=253.56 |
| and declaring class attributes for the constants | https://realpython.com/lessons/python-class-standard-library/#t=253.56 |
| in my enum. By convention, constants are written in all caps, | https://realpython.com/lessons/python-class-standard-library/#t=256.36 |
| so I’m sticking with that inside my enum declaration. | https://realpython.com/lessons/python-class-standard-library/#t=261.17 |
| 04:25 | https://realpython.com/lessons/python-class-standard-library/#t=265.57 |
| If you dig into the module, | https://realpython.com/lessons/python-class-standard-library/#t=265.57 |
| there are other ways to automatically assign values and shortcuts, | https://realpython.com/lessons/python-class-standard-library/#t=266.84 |
| but I’ll let you explore that on your own. With my enum class in hand, | https://realpython.com/lessons/python-class-standard-library/#t=270.57 |
| I of course can get at the attributes. | https://realpython.com/lessons/python-class-standard-library/#t=275.07 |
| 04:39 | https://realpython.com/lessons/python-class-standard-library/#t=279.85 |
| Note that that doesn’t just return 1. It’s its own thing. | https://realpython.com/lessons/python-class-standard-library/#t=279.85 |
| It actually is an enum object. That’s a bit weird, | https://realpython.com/lessons/python-class-standard-library/#t=284.4 |
| but you’ll see why in a second. | https://realpython.com/lessons/python-class-standard-library/#t=288.4 |
| 04:50 | https://realpython.com/lessons/python-class-standard-library/#t=290.92 |
| The purpose of an enum is grouping constants, so assigning a value | https://realpython.com/lessons/python-class-standard-library/#t=290.92 |
| appropriately gives an error. | https://realpython.com/lessons/python-class-standard-library/#t=298.03 |
| You can cast an enum to a list, and that list will contain | https://realpython.com/lessons/python-class-standard-library/#t=300.78 |
| all of its instances. You can construct the class with a value, | https://realpython.com/lessons/python-class-standard-library/#t=305.67 |
| and you’ll get back the corresponding enum object. | https://realpython.com/lessons/python-class-standard-library/#t=311.92 |
| 05:15 | https://realpython.com/lessons/python-class-standard-library/#t=315.4 |
| You can also look values up by name. | https://realpython.com/lessons/python-class-standard-library/#t=315.4 |
| 05:20 | https://realpython.com/lessons/python-class-standard-library/#t=320.76 |
| Remember when I said there was a reason you get back an enum object instead of | https://realpython.com/lessons/python-class-standard-library/#t=320.76 |
| just the value? Well, | https://realpython.com/lessons/python-class-standard-library/#t=323.84 |
| it’s because you can get at all the information associated with each part of the | https://realpython.com/lessons/python-class-standard-library/#t=325.15 |
| enum. Let me show you what I mean. | https://realpython.com/lessons/python-class-standard-library/#t=329.59 |
| 05:35 | https://realpython.com/lessons/python-class-standard-library/#t=335.35 |
| The .name attribute contains a string version of the enum member. | https://realpython.com/lessons/python-class-standard-library/#t=335.35 |
| 05:42 | https://realpython.com/lessons/python-class-standard-library/#t=342.86 |
| Well, .value contains the value. | https://realpython.com/lessons/python-class-standard-library/#t=342.86 |
| This allows you to go back and forth between the name, the value, | https://realpython.com/lessons/python-class-standard-library/#t=345.74 |
| and the enum object in your code as you need. | https://realpython.com/lessons/python-class-standard-library/#t=348.57 |
| 05:53 | https://realpython.com/lessons/python-class-standard-library/#t=353.87 |
| If I got a nickel for each time I said everything in Python is an object, | https://realpython.com/lessons/python-class-standard-library/#t=353.87 |
| I’d probably have enough for some candy in this course alone. Mmm, candy. | https://realpython.com/lessons/python-class-standard-library/#t=358.7 |
| 06:04 | https://realpython.com/lessons/python-class-standard-library/#t=364.54 |
| You may not have ever stopped and thought about what happens when you call a | https://realpython.com/lessons/python-class-standard-library/#t=364.54 |
| function, | https://realpython.com/lessons/python-class-standard-library/#t=367.84 |
| but in Python’s syntax, that parentheses on the end of the function are what is | https://realpython.com/lessons/python-class-standard-library/#t=368.5 |
| actually causing it to be called. But as everything is an object, | https://realpython.com/lessons/python-class-standard-library/#t=373.0 |
| even functions, using those parentheses to call something can be applied | https://realpython.com/lessons/python-class-standard-library/#t=377.43 |
| to an object as well. And in case you might have guessed, yep, | https://realpython.com/lessons/python-class-standard-library/#t=382.26 |
| there’s a dunder method for that. | https://realpython.com/lessons/python-class-standard-library/#t=386.91 |
| 06:29 | https://realpython.com/lessons/python-class-standard-library/#t=389.23 |
| When you stick parentheses on the end of an object, | https://realpython.com/lessons/python-class-standard-library/#t=389.23 |
| you’re calling it, and Python invokes the .__call__() method for you. | https://realpython.com/lessons/python-class-standard-library/#t=391.4 |
| A neat little aspect of this is many of those things you thought were functions | https://realpython.com/lessons/python-class-standard-library/#t=396.76 |
| are not. All the conversion calls? Yep, not functions. | https://realpython.com/lessons/python-class-standard-library/#t=400.54 |
| 06:45 | https://realpython.com/lessons/python-class-standard-library/#t=405.23 |
| They’re classes that have implemented .__call__(). | https://realpython.com/lessons/python-class-standard-library/#t=405.23 |
| In part one of this course, I spoke about using class methods for factories. | https://realpython.com/lessons/python-class-standard-library/#t=408.3 |
| These are essentially doing the same thing using that .__call__() method. | https://realpython.com/lessons/python-class-standard-library/#t=412.45 |
| 06:57 | https://realpython.com/lessons/python-class-standard-library/#t=417.23 |
| Same goes for iteration mechanisms like range() and enumerate(). | https://realpython.com/lessons/python-class-standard-library/#t=417.23 |
| In fact, | https://realpython.com/lessons/python-class-standard-library/#t=422.14 |
| over half the built-in so-called functions aren’t actually functions. | https://realpython.com/lessons/python-class-standard-library/#t=422.99 |
| 07:09 | https://realpython.com/lessons/python-class-standard-library/#t=429.4 |
| I hope you’re with me and not feeling dunder | https://realpython.com/lessons/python-class-standard-library/#t=429.4 |
| the weather. Told you I can do this all day. Next up, | https://realpython.com/lessons/python-class-standard-library/#t=431.86 |
| let’s be lazy and partially implement some classes, letting someone else finish | https://realpython.com/lessons/python-class-standard-library/#t=435.93 |
| the job for us. | https://realpython.com/lessons/python-class-standard-library/#t=440.42 |
| Become a Member | https://realpython.com/account/join/ |
| https://realpython.com/lessons/python-class-special-methods/ |
| Overview | https://realpython.com/courses/python-class-inheritance/ |
| https://realpython.com/lessons/python-class-abstracts-and-interfaces/ |
|
Inheritance and Internals: OOP in Python (Overview) 03:38
| https://realpython.com/videos/python-class-inheritance-overview/ |
|
Inheritance Inside Python 09:07
| https://realpython.com/videos/inheritance-in-python/ |
|
Multiple Inheritance - Multiple Parents 09:30
| https://realpython.com/lessons/python-multiple-inheritance/ |
|
Class Internals 08:58
| https://realpython.com/lessons/python-class-internals/ |
|
More Special Methods 06:50
| https://realpython.com/lessons/python-class-special-methods/ |
|
Classes in the Standard Library 07:23
| https://realpython.com/lessons/python-class-standard-library/ |
|
Abstracts and Interfaces 08:41
| https://realpython.com/lessons/python-class-abstracts-and-interfaces/ |
|
Inheritance and Internals: OOP in Python (Summary) 02:21
| https://realpython.com/lessons/python-class-inheritance-summary/ |
| Privacy Policy | https://realpython.com/privacy-policy/ |
Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover