.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/oop/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_methods.py: ============ 3.4 methods ============ This lesson discusses the concept of method in python. .. GENERATED FROM PYTHON SOURCE LINES 11-14 Forget for the time being what the word `methods` mean in English. The term `method` here means `functions associated with classes` Let's write a simple function which takes an `object` as input and prints the `name` attribute of that object" .. GENERATED FROM PYTHON SOURCE LINES 16-21 .. code-block:: default def say_salam(obj): print("Salam, I am " + obj.name + "!") return .. GENERATED FROM PYTHON SOURCE LINES 22-23 Let's also define a simple class named `Insan` as we did in previous examples. .. GENERATED FROM PYTHON SOURCE LINES 25-29 .. code-block:: default class Insan: pass .. GENERATED FROM PYTHON SOURCE LINES 30-31 We can use the function `salam` by using the instance of class `Insan` which is `x`. .. GENERATED FROM PYTHON SOURCE LINES 33-38 .. code-block:: default x = Insan() x.name = "Ali" say_salam(x) # >> Salam, I am Ali! .. rst-class:: sphx-glr-script-out .. code-block:: none Salam, I am Ali! .. GENERATED FROM PYTHON SOURCE LINES 39-40 ``say_salam`` is a function at this time, and we can verify this by checking its type .. GENERATED FROM PYTHON SOURCE LINES 42-45 .. code-block:: default type(say_salam) .. GENERATED FROM PYTHON SOURCE LINES 46-47 but we can bind/link it to class `Insan` as following .. GENERATED FROM PYTHON SOURCE LINES 50-58 .. code-block:: default def say_salam(obj): print("Salam, I am " + obj.name) class Insan: taruf = say_salam .. GENERATED FROM PYTHON SOURCE LINES 59-61 Now we can make use of the function ``say_salam`` which is linked to class ``Insan`` as following .. GENERATED FROM PYTHON SOURCE LINES 63-70 .. code-block:: default x = Insan() x.name = "Ali" Insan.taruf(x) .. rst-class:: sphx-glr-script-out .. code-block:: none Salam, I am Ali .. GENERATED FROM PYTHON SOURCE LINES 71-72 attributes in the ``Insan`` class are: .. GENERATED FROM PYTHON SOURCE LINES 74-77 .. code-block:: default print(Insan.__dict__) .. rst-class:: sphx-glr-script-out .. code-block:: none {'__module__': '__main__', 'taruf': , '__dict__': , '__weakref__': , '__doc__': None} .. GENERATED FROM PYTHON SOURCE LINES 78-79 `taruf` is a method and can be called as .. GENERATED FROM PYTHON SOURCE LINES 81-84 .. code-block:: default x.taruf() .. rst-class:: sphx-glr-script-out .. code-block:: none Salam, I am Ali .. GENERATED FROM PYTHON SOURCE LINES 85-95 so `Insan.taruf` and `x.taruf` are equivalent. Although the method `say_salam` takes one argument ``obj`` as input but we did not provide any input argument while calling it through ``x.taruf()`` and no error was thrown? This is because when the function ``say_salam`` was linked to the class ``Insan``, and when we call this method, the first argument is by default the instance i.e. ``x`` in this case. In this case we defined the method outside the body of class ``Insan`` and then linked it to class, but this is not the usual way to define methods. The proper way is to define it inside the class(indented) We connect this function which is method now to the class by its first argument which is usually ``self``. ``self`` corresponds to the ``Insan`` object ``x`` .. GENERATED FROM PYTHON SOURCE LINES 97-106 .. code-block:: default class Insan: def say_salam(self): print("Salam, I am " + self.name) return ali = Insan() .. GENERATED FROM PYTHON SOURCE LINES 107-109 uncomment following line ali.say_salam() # AttributeError .. GENERATED FROM PYTHON SOURCE LINES 111-113 The problem with above code is that the class ``Insan`` does not have an attribute ``name``.So the class must have an attribute ``name`` before using the method ``say_salam``. .. GENERATED FROM PYTHON SOURCE LINES 115-118 .. code-block:: default print(ali.__dict__) .. rst-class:: sphx-glr-script-out .. code-block:: none {} .. GENERATED FROM PYTHON SOURCE LINES 119-120 Lets define the attribute before using the method ``say_salam`` .. GENERATED FROM PYTHON SOURCE LINES 122-133 .. code-block:: default class Insan: def say_salam(self): print("Salam, I am " + self.name) ali = Insan() ali.name = 'Ali' # define an attribute of instance ali ali.say_salam() .. rst-class:: sphx-glr-script-out .. code-block:: none Salam, I am Ali .. GENERATED FROM PYTHON SOURCE LINES 134-137 .. code-block:: default print(ali.__dict__) .. rst-class:: sphx-glr-script-out .. code-block:: none {'name': 'Ali'} .. GENERATED FROM PYTHON SOURCE LINES 138-141 so whats the difference btw ``method`` and ``function``? ``self`` is just a convention, we can use ``this``, ``apna`` or any other keyword but it is better to just follow the convention so that others can follow your work .. GENERATED FROM PYTHON SOURCE LINES 144-155 .. code-block:: default class Insan: def say_salam(apna): print("Salam, I am " + apna.name) ali = Insan() ali.name = 'Ali' # define an attribute of instance ali ali.say_salam() .. rst-class:: sphx-glr-script-out .. code-block:: none Salam, I am Ali .. GENERATED FROM PYTHON SOURCE LINES 156-161 As said earlier, defining class attributes outside class is not proper way. We need the class ``Insan`` to have the attribute ``name`` before using the method ``say_salam``. So we need a systematic way that the class, upon its creation (initialization) must have the attribute ``name``.This is done with ``init`` method, which we will learn in next lesson. .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.004 seconds) .. _sphx_glr_download_auto_examples_oop_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/methods.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: methods.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: methods.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_