init, repr, str, cmp, new , del, hash, nonzero, unicode, class operators
__init__The task of constructors is to initialize(assign values) to the data members of the class when an object of class is created.repr()The repr() function returns a printable representation of the given object.- The
__str__method in Python represents the class objects as a string – it can be used for classes. The str method should be defined in a way that is easy to read and outputs all the members of the class. This method is also used as a debugging tool when the members of a class need to be checked. __cmp__is no longer used.__mew__Whenever a class is instantiated__new__and__init__methods are called.__new__method will be called when an object is created and__init__method will be called to initialize the object.-
__hash__() - Rich comparison methods
__call____contains__for i in self:
class A(object):
def __new__(cls):
print("Creating instance")
return super(A, cls).__new__(cls)
def __init__(self):
print("Init is called")
# Creating instance
# Init is calledclass A(object):
def __init__(self, a, b, c):
self._a = a
self._b = b
self._c = c
def __eq__(self, othr):
return (isinstance(othr, type(self))
and (self._a, self._b, self._c) ==
(othr._a, othr._b, othr._c))
def __hash__(self):
return hash((self._a, self._b, self._c))__lt__, __gt__, __le__, __ge__, __eq__, and __ne__
def __lt__(self, other):
...
def __le__(self, other):
...
def __gt__(self, other):
...
def __ge__(self, other):
...
def __eq__(self, other):
...
def __ne__(self, other):
...object() is shorthand for object.__call__()
class Product:
def __init__(self):
print("Instance Created")
# Defining __call__ method
def __call__(self, a, b):
print(a * b)
# Instance created
ans = Product()
# __call__ method will be called
ans(10, 20)