.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/oop/static_methods.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_static_methods.py: =================== 3.9 static methods =================== In previous section we wrote a code to count number of instances of class using a public attribute of a class. If we make this attribute `private` , we can create a method inside the class to acquire its value. In following example, we do it by ``PopulationCount`` method. .. GENERATED FROM PYTHON SOURCE LINES 13-30 .. code-block:: default class Insan: __counter = 0 def __init__(self): type(self).__counter += 1 def PopulationCount(self): return Insan.__counter ali = Insan() print(ali.PopulationCount()) hasan = Insan() print(hasan.PopulationCount()) .. rst-class:: sphx-glr-script-out .. code-block:: none 1 2 .. GENERATED FROM PYTHON SOURCE LINES 31-35 So far so good. But we have a problem, first, the ``PopulationCount`` does not have anything to do with the instance of class `Insan` i.e. `ali` and second, if we want access this method directly from class' instance, we will encounter an error as shown below. .. GENERATED FROM PYTHON SOURCE LINES 37-41 .. code-block:: default # uncomment following line # Insan.PopulationCount() # TypeError .. GENERATED FROM PYTHON SOURCE LINES 42-49 This is because, the method ``PopulationCount`` takes one input argument ``self``. When we call this method using instance of class, python implicit puts the class' instance, ``ali`` in this case, as an input argument but now we are not calling this method from instance, so python does not provide the implicit first input argument to this method. We could however, avoid this error by explicitly providing the instance ``ali`` as first input argument. .. GENERATED FROM PYTHON SOURCE LINES 51-54 .. code-block:: default Insan.PopulationCount(ali) .. rst-class:: sphx-glr-script-out .. code-block:: none 2 .. GENERATED FROM PYTHON SOURCE LINES 55-57 However, this is not a good way. An alternative would be to avoid the ``self`` statement when defining the method in the class. .. GENERATED FROM PYTHON SOURCE LINES 60-73 .. code-block:: default class Insan: __counter = 0 def __init__(self): type(self).__counter += 1 def PopulationCount(): return Insan.__counter Insan.PopulationCount() .. rst-class:: sphx-glr-script-out .. code-block:: none 0 .. GENERATED FROM PYTHON SOURCE LINES 74-78 But now we can't access this method from the instance, because when we access this method from instance, python implicitly gives instance `ali` as first input argument to this method but the method does not take any input argument as the error message also depicts this. .. GENERATED FROM PYTHON SOURCE LINES 80-85 .. code-block:: default # uncomment following two lines # ali = Insan() # TypeError # ali.PopulationCount() .. GENERATED FROM PYTHON SOURCE LINES 86-88 The way to solve this problem is to put the decorate `@staticmethod`. By doing so, python will not put the instance implicitly as first input argument. .. GENERATED FROM PYTHON SOURCE LINES 91-110 .. code-block:: default class Insan: __counter = 0 def __init__(self): type(self).__counter += 1 @staticmethod def PopulationCount(): return Insan.__counter print(Insan.PopulationCount()) ali = Insan() print(ali.PopulationCount()) hasan = Insan() print(hasan.PopulationCount()) print(Insan.PopulationCount()) .. rst-class:: sphx-glr-script-out .. code-block:: none 0 1 2 2 .. GENERATED FROM PYTHON SOURCE LINES 111-115 why to use them? ------------------ Static methods are used to group a utility function with a class. .. GENERATED FROM PYTHON SOURCE LINES 117-135 .. code-block:: default class Dates: def __init__(self, date): self.date = date def getDate(self): return self.date @staticmethod def toDashDate(date): return date.replace("/", "-") date = Dates("15-12-2016") dateFromDB = "15/12/2016" dateWithDash = Dates.toDashDate(dateFromDB) .. GENERATED FROM PYTHON SOURCE LINES 136-143 since ``toDashDate`` works only for dates, it's logical to keep it inside the Dates class Consider another example. Suppose we have a large number of examples which perform mathematical functions such as ``ceil``, ``multiply``, ``exponent``, ``divide`` on some input arguments. Then it would be logical to just group all such functions one one class such as ``math`` and use the functions as ``math.ceil(2.2)`` or ``math.exp(2.3)`` etc. .. GENERATED FROM PYTHON SOURCE LINES 145-162 .. code-block:: default class math(): @staticmethod def ceil(x): # perform ceil operation return @staticmethod def exp(x): # perform exponent operation return # more methods .. GENERATED FROM PYTHON SOURCE LINES 163-164 This increases code readability. .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.003 seconds) .. _sphx_glr_download_auto_examples_oop_static_methods.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/static_methods.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: static_methods.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: static_methods.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_