Skip to content

Instantly share code, notes, and snippets.

@Aditya-Jyoti
Last active May 31, 2021 10:19
Show Gist options
  • Save Aditya-Jyoti/64f746dc17b677d9a8413f5c1202fffd to your computer and use it in GitHub Desktop.
Save Aditya-Jyoti/64f746dc17b677d9a8413f5c1202fffd to your computer and use it in GitHub Desktop.
List of all Special Methods in Python OOP

List of all Special Methods in Python OOP

Initialization and Construction

Method Description
new(cls, other) To get called in an object's instantiation.
init(self, other) To get called by the __new__ method.
del(self) Destructor method.

 

Unary operators and functions

Method Description
pos(self) To get called for unary positive e.g. +someobject.
neg(self) To get called for unary negative e.g. -someobject.
abs(self) To get called by built-in abs() function.
invert(self) To get called for inversion using the ~ operator.
round(self,n) To get called by built-in round() function.
trunc(self) To get called by built-in math.trunc() function.
floor(self) To get called by built-in math.floor() function.
ceil(self) To get called by built-in math.ceil() function.

 

Augmented Assignment

Method Description
iadd(self, other) To get called on addition with assignment e.g. a +=b.
isub(self, other) To get called on subtraction with assignment e.g. a -=b.
imul(self, other) To get called on multiplication with assignment e.g. a *=b.
ifloordiv(self, other) To get called on integer division with assignment e.g. a //=b.
idiv(self, other) To get called on division with assignment e.g. a /=b.
itruediv(self, other) To get called on true division with assignment
imod(self, other) To get called on modulo with assignment e.g. a%=b.
ipow(self, other) To get called on exponentswith assignment e.g. a **=b.
ilshift(self, other) To get called on left bitwise shift with assignment e.g. a<<=b.
irshift(self, other) To get called on right bitwise shift with assignment e.g. a >>=b.
iand(self, other) To get called on bitwise AND with assignment e.g. a&=b.
ior(self, other) To get called on bitwise OR with assignment e.g. a
ixor(self, other) To get called on bitwise XOR with assignment e.g. a ^=b.

 

Type Conversion Magic Methods

Method Description
int(self) To get called by built-int int() method to convert a type to an int.
float(self) To get called by built-int float() method to convert a type to float.
complex(self) To get called by built-int complex() method to convert a type to complex.
oct(self) To get called by built-int oct() method to convert a type to octal.
hex(self) To get called by built-int hex() method to convert a type to hexadecimal.
index(self) To get called on type conversion to an int when the object is used in a slice expression.
trunc(self) To get called from math.trunc() method.

 

String Magic Methods

Method Description
str(self) To get called by built-int str() method to return a string representation of a type.
repr(self) To get called by built-int repr() method to return a machine readable representation of a type.
unicode(self) To get called by built-int unicode() method to return an unicode string of a type.
format(self, formatstr) To get called by built-int string.format() method to return a new style of string.
hash(self) To get called by built-int hash() method to return an integer.
nonzero(self) To get called by built-int bool() method to return True or False.
dir(self) To get called by built-int dir() method to return a list of attributes of a class.
sizeof(self) To get called by built-int sys.getsizeof() method to return the size of an object.

 

Attribute Magic Methods

Method Description
getattr(self, name) Is called when the accessing attribute of a class that does not exist.
setattr(self, name, value) Is called when assigning a value to the attribute of a class.
delattr(self, name) Is called when deleting an attribute of a class.

 

Operator Magic Methods

Method Description
add(self, other) To get called on add operation using + operator.
sub(self, other) To get called on subtraction operation using - operator.
mul(self, other) To get called on multiplication operation using * operator.
floordiv(self, other) To get called on floor division operation using // operator.
truediv(self, other) To get called on division operation using / operator.
mod(self, other) To get called on modulo operation using % operator.
pow(self, other[, modulo]) To get called on calculating the power using ** operator.
lt(self, other) To get called on comparison using < operator.
le(self, other) To get called on comparison using <= operator.
eq(self, other) To get called on comparison using == operator.
ne(self, other) To get called on comparison using != operator.
ge(self, other) To get called on comparison using >= operator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment