Specials#
The specials is a group of methods/attributes that have a special meaning for Python. They are wrapped double underscores, for example, __init__. Alternative names are magics and dunders (a shortcut for double underscore before and after).
Methods#
In classes, a set of methods with reserved names can be implemented. These methods are called when specific events occur with the class or its instances. Names of all this methods follows such pattern __<name>__, for example __init__, __repr__ and so on.
Find out more in the:
Some description in experimental approach in the corresponding page of this site.
The following table shows some special methods and their descriptions.
Group |
Method |
Description |
|---|---|---|
Object management |
Static method called to create a new instance |
|
|
Called immediately after instance creation |
|
|
Called when the instance is about to be destroyed - the correct abstract way to name it is “finalizer” |
|
Type Conversion |
|
Defines how an object should be converted into a string datatype. |
|
Defines how an object should be converted into a boolean datatype. |
|
|
Defines how an object should be converted into an integer datatype. |
|
|
Defines how an object should be converted into a float datatype. |
|
|
Defines how an object should be converted into a complex datatype. |
|
Arithmetic operators |
|
Compares two objects for equality ( |
|
Compares if the object is less than another ( |
|
|
Compares if the object is less than or equal to another ( |
|
|
Compares if the object is greater than another ( |
|
|
Compares if the object is greater than or equal to another ( |
|
|
Defines addition for objects ( |
|
|
Defines subtraction for objects ( |
|
|
Defines multiplication for objects ( |
|
|
Defines division for objects ( |
|
|
Defines floor division for objects ( |
|
|
Defines modulo operation for objects ( |
|
|
Defines power operation for objects ( |
|
|
Defines bitwise AND operation ( |
|
|
Defines bitwise OR operation (` |
|
|
Defines bitwise XOR operation ( |
|
|
Defines in-place addition ( |
|
|
Defines in-place subtraction ( |
|
|
Defines in-place multiplication ( |
|
|
Defines in-place division ( |
|
|
Defines unary negation ( |
|
|
Returns the absolute value of the object ( |
|
Indexing operator ( |
|
To try assigning a value to the index. |
|
To try to access value under the index. |
|
|
To try to delete value under the index |
|
Iteration protocol |
|
Will be called each time you need iterate over object. |
|
Will be called each time the object is passed to the |
|
Customizing class creation |
|
This method is called when ever the containing class is subclassed. |
As an example, consider a class that has the __getitem__ method defined. This method determines the behavior of the instances of the class when the [] operator is applied to them.
Here is how it works — it converts the literal 3 to the type of the input and applies + to the input and the transformed literal 3.
class TestClass:
def __getitem__(self, item):
return item + type(item)(3)
The following cell shows the behavior of the instance when 9 is passed to the [] operator.
TestClass()[6]
9
You can pass string literals as well.
TestClass()["hello"]
'hello3'
Attributes#
Actually, almost every instance in the Python environment has its own special attributes, but they come from different abstractions of the language:
Type attributes: Attributes of the type itself. The
typeclass, likeint, holds these attributes.Function attributes, Method attributes: Their purpose is clear from their names.
Instance attributes: Attributes that appear in an instance during its creation.
Category |
Attribute |
Description |
|---|---|---|
Type Attributes |
|
Name of the class. |
|
Module where the class is defined. |
|
|
Dictionary containing the class’s attributes and methods. |
|
|
Tuple of base classes (superclasses). |
|
|
Method Resolution Order (tuple of base classes in order). |
|
|
Returns a list of known subclasses. |
|
|
Docstring of the class (or |
|
|
Dictionary of variable type annotations. |
|
|
Fully qualified class name (e.g., |
|
Function Attributes |
|
Code object representing the function’s compiled bytecode. |
|
Tuple of default values for function parameters. |
|
|
Dictionary of default values for keyword-only arguments. |
|
|
Reference to the global namespace where the function is defined. |
|
|
Tuple of cell objects containing variables used in closures. |
|
|
Dictionary of function parameter and return type annotations. |
|
Method Attributes |
|
Reference to the underlying function of a method. |
|
Instance to which a bound method is attached. |
|
|
Docstring of the method. |
|
|
Dictionary of method parameter and return type annotations. |
|
Instance Attributes |
|
Dictionary containing instance attributes. |
|
Reference to the class of the instance. |
|
|
Tuple defining allowed instance attributes (if used). |
|
|
List of weak references to the object (if applicable). |
Check more specific description at the corresponding page.