Sobes.tech
Junior — Middle

Can you list the main built-in functions available for classes in Python?

sobes.tech AI

Answer from AI

In Python, classes have access to a number of built-in functions (methods) that allow working with objects and classes. Here are the main ones:

  • __init__(self, ...) — class constructor, called when creating an object.
  • __del__(self) — destructor, called when deleting an object.
  • __str__(self) — returns a string representation of the object for the str() function.
  • __repr__(self) — returns an official string representation of the object, useful for debugging.
  • __eq__(self, other) — defines the behavior of the equality operator ==.
  • __lt__(self, other), __gt__(self, other) and others — for comparing objects.
  • __len__(self) — returns the length of the object, used by the len() function.
  • __getitem__(self, key) — allows accessing elements of the object by index or key.
  • __setitem__(self, key, value) — allows setting elements by index or key.
  • __iter__(self) — returns an iterator for the object.

These methods can be overridden in your classes to customize object behavior.