Special methods are predefined methods of any class. We create objects like using range, zip, then these objects have certain predefined methods like next() or __next__. Special methods are recognized with the double underscores in front and double underscores after the method name.

Special methods are also called magic methods.Let’s see some of the examples -:

Checking the special methods of range object

## Creating a range object
x = range(4)
## Printing that object
x

Out[1]:

range(0, 4)

In[2]: Here, there is no next method associated with the range object. But if we will create a generator using range object then next()function will work because the generator has special method next.

next(x)

---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-13-92de4e9f6b1e> in <module>()
----> 1next(x)

StopIteration: 

In [3]:

Checking the magical/special methods using dir() function of range class.
print(dir(range))

['__bool__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index', 'start', 'step', 'stop']

In [4]: Creating a generator and checking the special methods

## Creating a generator 
x = (i for i in range(5))

In [5]:

## Printing and it will not print value, it will print generator object
x

Out[5]:

<generator object <genexpr> at 0x000001F0D9436048>

In [6]: Here next will work even generator is created using range because generator has __next__ method with it.

## Using the next method of generator class, can access the next value.
next(x)

Out[6]:

0

In [7]:

## Printing the other value using for loop
for i in x:
print(i)

1
2
3
4

In [8]: Checking the special methods of generator function.

print(dir(x))

['__class__', '__del__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__lt__', '__name__', '__ne__', '__new__', '__next__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'close', 'gi_code', 'gi_frame', 'gi_running', 'gi_yieldfrom', 'send', 'throw']

Creating a custom special method

In [9]: Creating a custom class with a special method in it.

## Creating a cutom class with a special method __specialprint__
class custom_class:
def __init__(self, name):
self.name = name

## Creating a special method which is just printing the name associated with the object
  def __specialprint__(self):
print(self.name)

In [10]:

## Creating the object
c_c = custom_class('Rajesh')

In [11]:

## Accessing the special method
c_c.__specialprint__()

Rajesh

In [12]: Checking the special methods associated with our custom class. So, some are default special methods of class and one of our created one

print(dir(custom_class))

Highlighted method below is the custom special method.

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

Hope you understand the special methods in Python i.e, how to create them. 

Stay tuned! Keep learning with us.

Leave a Comment

Your email address will not be published. Required fields are marked *