Pergunta de entrevista da empresa BairesDev

What are magic methods in Python and what are they used for?

Resposta da entrevista

Sigiloso

18 de dez. de 2018

These are special methods surrounded by double underscores, such as __init__, __repr__, __str___ __setitem__, __getitem__, __enter__, __exit__, __add__, __mult__, __next__ and so on... The interpreter invokes them in certain situations, such as when creating an object, converting it to string via built-in str() function. They are used for operator overloading, i.e. to make expressions such as a + b or a * b, or a[ i ] work for custom defined classes and not just for numbers, or arrays.