.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/oop/call.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_oop_call.py: ============= 3.14 __call__ ============= This lesson shows the usage of ``__call__`` method of a class. .. GENERATED FROM PYTHON SOURCE LINES 10-11 Suppose we have a function named `human`. .. GENERATED FROM PYTHON SOURCE LINES 11-18 .. code-block:: default def human(age): print(f"my age is {age}") return .. GENERATED FROM PYTHON SOURCE LINES 19-20 Since `human` is a function, we can **call** this function using ``()``. .. GENERATED FROM PYTHON SOURCE LINES 20-23 .. code-block:: default human(12) .. rst-class:: sphx-glr-script-out .. code-block:: none my age is 12 .. GENERATED FROM PYTHON SOURCE LINES 24-27 What if we want a class (or an instance of a class) to behave like function. To be specific if we want to use an instance of a class as a function, so that when we can call this instance, we will write ``__call__`` method of the class. .. GENERATED FROM PYTHON SOURCE LINES 27-39 .. code-block:: default class Human: def __init__(self, age): self.age = age def __call__(self): print(f"my age is {self.age}") return .. GENERATED FROM PYTHON SOURCE LINES 40-42 The `Human` class has two (user defined) methods i.e. ``__init__`` and ``__call__``. The ``__init__`` method is used/called when we initialize th class and create its instance. .. GENERATED FROM PYTHON SOURCE LINES 42-45 .. code-block:: default human = Human(12) .. GENERATED FROM PYTHON SOURCE LINES 46-48 Now if we use the instance `human` as function i.e. if we call `human` using ``()``, the ``__call__`` method will be executed. .. GENERATED FROM PYTHON SOURCE LINES 48-51 .. code-block:: default human() .. rst-class:: sphx-glr-script-out .. code-block:: none my age is 12 .. GENERATED FROM PYTHON SOURCE LINES 52-54 The ``__call__`` can be defined to take any keyword, non-keyword, optional or obligatory arguments. .. GENERATED FROM PYTHON SOURCE LINES 54-69 .. code-block:: default class Human: def __init__(self, age): self.age = age def __call__(self, characteristic): print(f"I am {characteristic}") return human = Human(63) human("mortal") .. rst-class:: sphx-glr-script-out .. code-block:: none I am mortal .. GENERATED FROM PYTHON SOURCE LINES 70-85 .. code-block:: default class Human: def __init__(self, age): self.age = age def __call__(self, *args, **kwargs): print(f"{args} {kwargs}") return human = Human(63) human() .. rst-class:: sphx-glr-script-out .. code-block:: none () {} .. GENERATED FROM PYTHON SOURCE LINES 86-88 .. code-block:: default human(1) .. rst-class:: sphx-glr-script-out .. code-block:: none (1,) {} .. GENERATED FROM PYTHON SOURCE LINES 89-91 .. code-block:: default human(1, a=2) .. rst-class:: sphx-glr-script-out .. code-block:: none (1,) {'a': 2} .. GENERATED FROM PYTHON SOURCE LINES 92-94 Without writing, ``__call__`` method for the `Human` class, we wouldn't be able to call the `Human` class or its instance `human`. .. GENERATED FROM PYTHON SOURCE LINES 96-99 **Question**: In the code ``Human(1)()``, what does the second braces ``()``, those which come after **(1)** represent? .. GENERATED FROM PYTHON SOURCE LINES 99-110 .. code-block:: default class Human: def __init__(self, age): self.age = age human = Human(2) # uncomment the following line # human() # -> TypeError: 'Human' object is not callable. .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.003 seconds) .. _sphx_glr_download_auto_examples_oop_call.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/AtrCheema/python-seekho/master?urlpath=lab/tree/notebooks/auto_examples/oop/call.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: call.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: call.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_