Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the Python documentation for the special methods? (__init__, __new__, __len__, ...)

Where is a complete list of the special double-underscore/dunder methods that can be used in classes? (e.g., __init__, __new__, __len__, __add__)

like image 586
mk12 Avatar asked Sep 13 '09 21:09

mk12


People also ask

What are special methods in Python?

In Python, special methods are a set of predefined methods you can use to enrich your classes. They are easy to recognize because they start and end with double underscores, for example __init__ or __str__ .

What is __ Getitem __ in Python?

__getitem__() is a magic method in Python, which when used in a class, allows its instances to use the [] (indexer) operators. Say x is an instance of this class, then x[i] is roughly equivalent to type(x). __getitem__(x, i) .

What are Python magic methods?

Magic methods in Python are the special methods that start and end with the double underscores. They are also called dunder methods. Magic methods are not meant to be invoked directly by you, but the invocation happens internally from the class on a certain action.


1 Answers

Please take a look at the special method names section in the Python language reference.

like image 55
Martin Geisler Avatar answered Sep 28 '22 07:09

Martin Geisler