Back to search

Common special methods like `__str__`, `__repr__`, `__len__`, `__getitem__`, and `__contains__`

Special methods let your objects behave like built-in types in printing, indexing, length checks, and membership operations.

Python Medium Theory

Common special methods like `__str__`, `__repr__`, `__len__`, `__getitem__`, and `__contains__`

Special methods let your objects behave like built-in types in printing, indexing, length checks, and membership operations.

  • `__str__` is user-friendly output
  • `__repr__` should be unambiguous
  • Container protocols come from dunder methods

Common special methods like `__str__`, `__repr__`, `__len__`, `__getitem__`, and `__contains__`