🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

Python Intermediate

What is Magic Methods?

Special double-underscore methods that define how Python objects behave with built-in operations and functions.

Magic methods (dunder methods) customize object behavior. __init__ initializes objects. __str__ defines string representation. __len__ enables len(). __getitem__ supports indexing. __add__ defines + operator behavior.

Other important ones include __enter__/__exit__ (context managers), __iter__/__next__ (iterators), __call__ (callable objects), and __eq__/__lt__ (comparison). Understanding magic methods is key to Pythonic class design.

Related Terms

Python Wheel
A built distribution format (.whl) that allows faster installation of Python packages by avoiding the need for compilation.
Context Manager
An object that manages resources by defining setup and cleanup actions using the with statement.
Lambda Function
An anonymous, single-expression function defined inline using the lambda keyword.
Python Metaclass
A class whose instances are classes themselves — the class of a class that controls how classes are created and behaved.
Dataclass
A decorator that automatically generates __init__, __repr__, __eq__, and other special methods for classes that mainly store data.
NumPy
A fundamental library for numerical computing in Python, providing efficient multi-dimensional arrays and mathematical operations.
View All Python Terms →