OOP in Python

Classes


Instances


Methods

What is self?


Magic Methods (Dunder Methods)

Magic methods let you define how objects of your class behave with Python's built-in operations. Some important ones include:

  • __str__ and __repr__ - Control how your object is printed

  • __len__ - Define what len(object) returns

  • __add__, __sub__, etc. - Enable math operators like + and -

  • __eq__, __lt__, etc. - Enable comparison operators

  • __getitem__ and __setitem__ - Make your object indexable like a list

  • __call__ - Make your object callable like a function


abc - Abstract Base Class


Last updated