13.1 Intro to Special Methods

Intro to Special Methods

Special methods (occasionally called Magic Methods) make your code more elegant and intuitive.

We will go over a couple of special methods in python. In previous lessons, we have learned that any class we create is a type of object. The object superclass allows a user to define a variety of methods in custom classes. Today, we will be going over a few key functions in Python that can be implemented in a class. This will include __str__, __init__, mathematical operators, and comparison operators.


Notice that all special methods are enclosed in underscores, so they will always look like: __functionName__

Each of these operators allows us to write concise and short code, which allows us to write compact programs without requiring us to make additional helper methods. We will start out with the most fundamental special methods.

Next Section


Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.