|
| https://realpython.com/ |
| Start Here | https://realpython.com/start-here/ |
|
Learn Python
| https://realpython.com/lessons/python-class-special-methods/ |
| 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-special-methods/ |
| 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-special-methods%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-special-methods/#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-special-methods/#transcript |
| Discussion (3) | https://realpython.com/lessons/python-class-special-methods/#discussion |
| 00:00 | https://realpython.com/lessons/python-class-special-methods/#t=0.58 |
| In the previous lesson, I showed you even more about descriptors than was covered | https://realpython.com/lessons/python-class-special-methods/#t=0.58 |
| in part one of the course and gave you an overview of .__slots__. In this lesson, | https://realpython.com/lessons/python-class-special-methods/#t=4.0 |
| I’ll continue down the dunder rabbit hole, and if you’re lucky, | https://realpython.com/lessons/python-class-special-methods/#t=8.79 |
| you’ll see a smiling cat. Is an Alice in Wonderland reference too obscure? | https://realpython.com/lessons/python-class-special-methods/#t=12.06 |
| 00:18 | https://realpython.com/lessons/python-class-special-methods/#t=18.32 |
| Continuing on our journey to dunder all the things, | https://realpython.com/lessons/python-class-special-methods/#t=18.32 |
| let me remind you that everything in Python is an object. | https://realpython.com/lessons/python-class-special-methods/#t=21.69 |
| So when you go to do key things like operations, | https://realpython.com/lessons/python-class-special-methods/#t=25.33 |
| they are actually implemented with methods. They’d have to be ‘cause, | https://realpython.com/lessons/python-class-special-methods/#t=27.98 |
| well, they’re objects. And using dunder methods, you can perform | https://realpython.com/lessons/python-class-special-methods/#t=32.1 |
| operations like adding, subtracting, in fact | https://realpython.com/lessons/python-class-special-methods/#t=37.04 |
| all the math things, as well as bitwise operations, convert things … | https://realpython.com/lessons/python-class-special-methods/#t=40.65 |
| You’ve seen .__str__() and .__repr__() in previous parts of this course, | https://realpython.com/lessons/python-class-special-methods/#t=45.73 |
| and this goes for all conversion. | https://realpython.com/lessons/python-class-special-methods/#t=50.45 |
| 00:52 | https://realpython.com/lessons/python-class-special-methods/#t=52.3 |
| Calling bool() or int() or float() invokes a corresponding dunder method. That also | https://realpython.com/lessons/python-class-special-methods/#t=52.3 |
| goes for comparison. | https://realpython.com/lessons/python-class-special-methods/#t=57.41 |
| All the things you might do in an if statement, like comparing for equals, are | https://realpython.com/lessons/python-class-special-methods/#t=59.32 |
| done with dunder underneath. | https://realpython.com/lessons/python-class-special-methods/#t=62.7 |
| 01:05 | https://realpython.com/lessons/python-class-special-methods/#t=65.04 |
| The cool thing about all this is you can override these methods in your own | https://realpython.com/lessons/python-class-special-methods/#t=65.04 |
| objects and be able to use these language features on the objects themselves. | https://realpython.com/lessons/python-class-special-methods/#t=68.57 |
| 01:14 | https://realpython.com/lessons/python-class-special-methods/#t=74.19 |
| Let’s go visit the Great Down Dunder. No? | https://realpython.com/lessons/python-class-special-methods/#t=74.19 |
| Enter the Dunder Dome. Dunder the Boardwalk? Okay, | https://realpython.com/lessons/python-class-special-methods/#t=78.23 |
| I’ll stop now. Nah, one more. Dunderworld, | https://realpython.com/lessons/python-class-special-methods/#t=82.77 |
| a movie about tight pants and object methods. Entertain me. | https://realpython.com/lessons/python-class-special-methods/#t=86.14 |
| 01:29 | https://realpython.com/lessons/python-class-special-methods/#t=89.55 |
| Leave one of your own in the comments. | https://realpython.com/lessons/python-class-special-methods/#t=89.55 |
| 01:33 | https://realpython.com/lessons/python-class-special-methods/#t=93.36 |
| A common reason to override operations is to implement classes where those | https://realpython.com/lessons/python-class-special-methods/#t=93.36 |
| operations are natural. | https://realpython.com/lessons/python-class-special-methods/#t=97.2 |
| If you want to build math concepts like matrices or vectors, | https://realpython.com/lessons/python-class-special-methods/#t=99.1 |
| there’s no reason why you shouldn’t be able to take advantage of the math | https://realpython.com/lessons/python-class-special-methods/#t=102.2 |
| operators, adding two of them together, for example. Of course, | https://realpython.com/lessons/python-class-special-methods/#t=105.68 |
| most of this is already in the standard library or third-party libraries like | https://realpython.com/lessons/python-class-special-methods/#t=109.12 |
| NumPy, so don’t write it yourself, but if you wanted to, | https://realpython.com/lessons/python-class-special-methods/#t=112.48 |
| this is what it’d look like. | https://realpython.com/lessons/python-class-special-methods/#t=115.83 |
| 01:58 | https://realpython.com/lessons/python-class-special-methods/#t=118.09 |
| A Vector class takes an .x and .y component, and .__add__() is what you came here | https://realpython.com/lessons/python-class-special-methods/#t=118.09 |
| to see. Overriding this method makes the Vector class work with the add operator. | https://realpython.com/lessons/python-class-special-methods/#t=123.05 |
| 02:08 | https://realpython.com/lessons/python-class-special-methods/#t=128.71 |
| The signature takes on one other object, | https://realpython.com/lessons/python-class-special-methods/#t=128.71 |
| the thing being added to this instance. | https://realpython.com/lessons/python-class-special-methods/#t=131.35 |
| The result of .__add__() should be a new vector with its attributes being the | https://realpython.com/lessons/python-class-special-methods/#t=134.67 |
| addition of this vector and the one passed in. To make this visible in | https://realpython.com/lessons/python-class-special-methods/#t=139.13 |
| the REPL, I’ve also implemented .__repr__(). | https://realpython.com/lessons/python-class-special-methods/#t=144.08 |
| 02:27 | https://realpython.com/lessons/python-class-special-methods/#t=147.74 |
| Of course, this class is incomplete. | https://realpython.com/lessons/python-class-special-methods/#t=147.74 |
| A good Vector class would implement all the math stuff, | https://realpython.com/lessons/python-class-special-methods/#t=149.84 |
| but this is enough so that you get the idea. Importing … | https://realpython.com/lessons/python-class-special-methods/#t=152.9 |
| 02:41 | https://realpython.com/lessons/python-class-special-methods/#t=161.09 |
| creating a vector … looking at the vector, | https://realpython.com/lessons/python-class-special-methods/#t=161.09 |
| which called .__repr__() … | https://realpython.com/lessons/python-class-special-methods/#t=165.84 |
| 02:50 | https://realpython.com/lessons/python-class-special-methods/#t=170.73 |
| creating another vector … and adding them together results in a third. | https://realpython.com/lessons/python-class-special-methods/#t=170.73 |
| .__add__() for the win. | https://realpython.com/lessons/python-class-special-methods/#t=175.47 |
| It’s a Dunderful Life. I told you, leave a comment. | https://realpython.com/lessons/python-class-special-methods/#t=177.32 |
| 03:00 | https://realpython.com/lessons/python-class-special-methods/#t=180.98 |
| I’m not going to stop anytime soon. | https://realpython.com/lessons/python-class-special-methods/#t=180.98 |
| 03:04 | https://realpython.com/lessons/python-class-special-methods/#t=184.74 |
| There are a ton of these dunders in the base object, | https://realpython.com/lessons/python-class-special-methods/#t=184.74 |
| which you can override. Using dunder methods, | https://realpython.com/lessons/python-class-special-methods/#t=187.45 |
| you can create context managers, muck around with dynamic class creation | https://realpython.com/lessons/python-class-special-methods/#t=190.23 |
| and instantiation control, | https://realpython.com/lessons/python-class-special-methods/#t=195.11 |
| check whether something is inside something else, is an instance of something | https://realpython.com/lessons/python-class-special-methods/#t=196.94 |
| else, or how long it is, control what happens when the format() function is called. | https://realpython.com/lessons/python-class-special-methods/#t=201.45 |
| 03:27 | https://realpython.com/lessons/python-class-special-methods/#t=207.03 |
| In a previous lesson, you saw how to control some of those attribute management | https://realpython.com/lessons/python-class-special-methods/#t=207.03 |
| things, but it goes much further. There are multiple mechanisms for getting, | https://realpython.com/lessons/python-class-special-methods/#t=210.89 |
| setting, | https://realpython.com/lessons/python-class-special-methods/#t=214.89 |
| adding, and removing attributes, and that’s not it. | https://realpython.com/lessons/python-class-special-methods/#t=215.39 |
| 03:40 | https://realpython.com/lessons/python-class-special-methods/#t=220.26 |
| There are over 125 dunder methods and attributes on the base class, | https://realpython.com/lessons/python-class-special-methods/#t=220.26 |
| which you automatically inherit just by writing a class definition. | https://realpython.com/lessons/python-class-special-methods/#t=224.34 |
| There are so many that if you laid them end to end, | https://realpython.com/lessons/python-class-special-methods/#t=228.66 |
| you could use a submarine to travel 20,000 leagues—say it with me— | https://realpython.com/lessons/python-class-special-methods/#t=230.95 |
| dunder the sea. Before moving on to the next lesson, | https://realpython.com/lessons/python-class-special-methods/#t=235.87 |
| a quick little tangent about things that can go wrong. | https://realpython.com/lessons/python-class-special-methods/#t=240.56 |
| 04:03 | https://realpython.com/lessons/python-class-special-methods/#t=243.43 |
| Depending on what you’re doing with a class, | https://realpython.com/lessons/python-class-special-methods/#t=243.43 |
| you can see different kinds of errors. | https://realpython.com/lessons/python-class-special-methods/#t=245.3 |
| An AttributeError is raised if you attempt to access an attribute that’s not on | https://realpython.com/lessons/python-class-special-methods/#t=247.78 |
| the object or class. A TypeError is raised if you’re trying to do an | https://realpython.com/lessons/python-class-special-methods/#t=251.88 |
| operation on the object that isn’t implemented. For example, calling len(), | https://realpython.com/lessons/python-class-special-methods/#t=256.52 |
| which invokes .__len__() on an integer. And some classes that | https://realpython.com/lessons/python-class-special-methods/#t=261.17 |
| expect to be extended will define a method and have it raise a NotImplementedError. | https://realpython.com/lessons/python-class-special-methods/#t=265.96 |
| 04:30 | https://realpython.com/lessons/python-class-special-methods/#t=270.0 |
| That way, if the extender didn’t override it, | https://realpython.com/lessons/python-class-special-methods/#t=270.0 |
| you’d get this exception. | https://realpython.com/lessons/python-class-special-methods/#t=273.85 |
| There’s another way of accomplishing this, though, called an abstract base class, | https://realpython.com/lessons/python-class-special-methods/#t=275.95 |
| which I’ll cover in a later lesson. If you’re writing classes, | https://realpython.com/lessons/python-class-special-methods/#t=279.53 |
| there are a few common mistakes to look out for. | https://realpython.com/lessons/python-class-special-methods/#t=284.08 |
| 04:46 | https://realpython.com/lessons/python-class-special-methods/#t=286.8 |
| Forgetting to include the self argument in a method will cause either an | https://realpython.com/lessons/python-class-special-methods/#t=286.8 |
| exception if you had no arguments at all, or having a reference to the object | https://realpython.com/lessons/python-class-special-methods/#t=290.51 |
| stuffed in whatever you wrote as the first argument. | https://realpython.com/lessons/python-class-special-methods/#t=294.75 |
| 04:58 | https://realpython.com/lessons/python-class-special-methods/#t=298.43 |
| I tend to make this mistake when I decide a function that I’ve written really | https://realpython.com/lessons/python-class-special-methods/#t=298.43 |
| shouldn’t be a function and turn it into a method, moving it inside my class. | https://realpython.com/lessons/python-class-special-methods/#t=301.87 |
| 05:06 | https://realpython.com/lessons/python-class-special-methods/#t=306.74 |
| Another case is confusing when you are supposed to be using the class itself | https://realpython.com/lessons/python-class-special-methods/#t=306.74 |
| vs an instance. This isn’t as common as the previous error, | https://realpython.com/lessons/python-class-special-methods/#t=310.65 |
| but can happen when you’re supposed to be passing around a class reference | https://realpython.com/lessons/python-class-special-methods/#t=315.17 |
| rather than instance reference to, say, a utility method or function. | https://realpython.com/lessons/python-class-special-methods/#t=318.43 |
| 05:23 | https://realpython.com/lessons/python-class-special-methods/#t=323.51 |
| Somewhat related is confusing what are class attributes and instance attributes. | https://realpython.com/lessons/python-class-special-methods/#t=323.51 |
| 05:29 | https://realpython.com/lessons/python-class-special-methods/#t=329.11 |
| I covered a tricky version of this bug in part one of the course, where I showed | https://realpython.com/lessons/python-class-special-methods/#t=329.11 |
| the accidental creation of an instance attribute instead of modifying an | https://realpython.com/lessons/python-class-special-methods/#t=333.33 |
| existing class attribute. Not quite mistakes, | https://realpython.com/lessons/python-class-special-methods/#t=337.99 |
| but some things that can lead to problematic code are using non-public members | https://realpython.com/lessons/python-class-special-methods/#t=341.88 |
| outside a class. | https://realpython.com/lessons/python-class-special-methods/#t=346.97 |
| 05:48 | https://realpython.com/lessons/python-class-special-methods/#t=348.48 |
| Things with leading underscores are a sign by the coder that they might change | https://realpython.com/lessons/python-class-special-methods/#t=348.48 |
| or have side effects that you’re unaware of. Generally, | https://realpython.com/lessons/python-class-special-methods/#t=352.36 |
| if you aren’t inside the class, you shouldn’t be using non-public members. | https://realpython.com/lessons/python-class-special-methods/#t=355.76 |
| 06:01 | https://realpython.com/lessons/python-class-special-methods/#t=361.1 |
| The entire third part of this course could be summed up in don’t misuse | https://realpython.com/lessons/python-class-special-methods/#t=361.1 |
| object-oriented relationships. | https://realpython.com/lessons/python-class-special-methods/#t=365.04 |
| Coders new to OO or coming from object-oriented heavy languages will tend to | https://realpython.com/lessons/python-class-special-methods/#t=367.54 |
| create oodles of objects. | https://realpython.com/lessons/python-class-special-methods/#t=371.68 |
| 06:13 | https://realpython.com/lessons/python-class-special-methods/#t=373.35 |
| This is often overkill in Python, and likewise, | https://realpython.com/lessons/python-class-special-methods/#t=373.35 |
| you can always tell a programmer who’s recently discovered the beauty of | https://realpython.com/lessons/python-class-special-methods/#t=377.26 |
| operator overloading when they tend to overload the operators to save typing. | https://realpython.com/lessons/python-class-special-methods/#t=380.76 |
| 06:25 | https://realpython.com/lessons/python-class-special-methods/#t=385.03 |
| Instead of writing a named method that is clear, | https://realpython.com/lessons/python-class-special-methods/#t=385.03 |
| they might override .__add__() instead. | https://realpython.com/lessons/python-class-special-methods/#t=387.55 |
| 06:30 | https://realpython.com/lessons/python-class-special-methods/#t=390.82 |
| My general advice here is stick with the intent of the operator. | https://realpython.com/lessons/python-class-special-methods/#t=390.82 |
| If you’re not actually adding things together, you shouldn’t override .__add__(). | https://realpython.com/lessons/python-class-special-methods/#t=395.02 |
| 06:39 | https://realpython.com/lessons/python-class-special-methods/#t=399.38 |
| Classes aren’t just for you. | https://realpython.com/lessons/python-class-special-methods/#t=399.38 |
| They’re for the core programmers as well. Next up, | https://realpython.com/lessons/python-class-special-methods/#t=403.18 |
| I’ll show you some classes in the standard library. | https://realpython.com/lessons/python-class-special-methods/#t=405.94 |
| Sept. 21, 2023 | https://realpython.com/lessons/python-class-special-methods/#comment-d3dece16-73dc-4987-bfa2-f696a39f3b6c |
| Sept. 21, 2023 | https://realpython.com/lessons/python-class-special-methods/#comment-241ccebd-0d4e-4821-8b3c-fea427b29923 |
| Oct. 23, 2025 | https://realpython.com/lessons/python-class-special-methods/#comment-177abee0-17ab-4453-a8a5-a2f1117a7d30 |
| Become a Member | https://realpython.com/account/join/ |
| https://realpython.com/lessons/python-class-internals/ |
| Overview | https://realpython.com/courses/python-class-inheritance/ |
| https://realpython.com/lessons/python-class-standard-library/ |
|
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