What are the Dunder/Magic/Special methods in Python? Name a few.

Technology Community › Category: Python › What are the Dunder/Magic/Special methods in Python? Name a few.
VietMX Staff asked 5 years ago

Dunder (derived from double underscore) methods are special/magic predefined methods in Python, with names that start and end with a double underscore. There’s nothing really magical about them. Examples of these include:

  • __init__ – constructor
  • __str__, __repr__ – object representation (casting to string, printing)
  • __len__, __next__… – generators
  • __enter__, __exit__ – context managers
  • __eq__, __lt__, __gt__ – operator overloading