Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the unique methods of a class called?

Say that if you had a class, and you defined it as:

class A:
    pass

And when you use the function:

dir(A)

Which returns all the methods and attributes the class contains:

['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']

My question is, what are methods with a double underscore before and after them such as __class__, __delattr__, __dict__, __init__, called? I've assumed they were called constructor, but after doing research - it seems that term only refers to the __init__ function. Does anyone know if there's a specific category to call these methods by?

like image 319
Blueboy90780 Avatar asked Mar 07 '23 12:03

Blueboy90780


2 Answers

They are called special method names:

Special method names

A class can implement certain operations that are invoked by special syntax (such as arithmetic operations or subscripting and slicing) by defining methods with special names. This is Python’s approach to operator overloading, allowing classes to define their own behavior with respect to language operators.

and special attributes:

Some of the type descriptions below contain a paragraph listing ‘special attributes.’ These are attributes that provide access to the implementation and are not intended for general use.

Names that start with a double underscore and end in a double underscore, are reserved names. See the Reserved classes of identifiers:

__*__
System-defined names. These names are defined by the interpreter and its implementation (including the standard library). Current system names are discussed in the Special method names section and elsewhere. More will likely be defined in future versions of Python. Any use of __*__ names, in any context, that does not follow explicitly documented use, is subject to breakage without warning.

They are sometimes referred to as dunder names, as an abbreviation of the double-underscore. Magic names is also a popular term for them, but neither are official.

Note that the __init__ method is the instance initialiser hook, it is not the constructor. The instance has already been constructed by the time this method is called. If you need to hook into construction, implement the __new__ method.

like image 129
Martijn Pieters Avatar answered Mar 30 '23 09:03

Martijn Pieters


These are comprised of the so called special methods and special attributes. That is the official name of these methods and attributes according to the language specification.

In the Python glossary you can see an entry for them that states:

special method

A method that is called implicitly by Python to execute a certain operation on a type, such as addition. Such methods have names starting and ending with double underscores. Special methods are documented in Special method names.

Magic and dunder (methods) are two other commonly used terms in the community.


As a side-note, it is worth mentioning that dir does not report on all of them.

like image 23
Dimitris Fasarakis Hilliard Avatar answered Mar 30 '23 08:03

Dimitris Fasarakis Hilliard