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

Technology CommunityCategory: PythonWhat are the Dunder/Magic/Special methods in Python? Name a few.
VietMX Staff asked 3 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