From the course: Python Object-Oriented Programming

What are magic methods?

- [Instructor] In this chapter, we're going to spend some time learning about Python's so-called "magic" methods, which are a set of methods that Python automatically associates with every class definition. Your classes can override these methods to customize a variety of behavior and make them act just like built-in Python classes. There are quite a few of these methods and I'm not going to cover all of them. Instead, I'm going to demonstrate the ones that are most useful and commonly employed in code that you'll run across. Using these methods, you can customize how your objects are represented as strings, both for display to the user and for debugging purposes. You can control how attributes are accessed on an object both for when they are set and when they're retrieved. You can add capabilities to your classes that enable them to be used in expressions such as testing for equality and other comparison operations like greater than or less than. And then finally, we'll see how to make an object callable just like a function and how that can be used to make code more concise and readable. Features like these are what gives Python its flexibility and power, and in this chapter we'll see examples of how they can be put to good use.

Contents