.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/oop/init.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_init.py: ================ 3.5 init method ================ .. GENERATED FROM PYTHON SOURCE LINES 8-13 This method is used to define the attributes of a class which we want that class to have right upon its creation. This means the code in ``init`` method is executed at the time of creation of an instance of class. Thus we use ``init`` method to initialize the class, hence name ``init``. It can be anywhere inside class but convention is to put it at the top inside a class. .. GENERATED FROM PYTHON SOURCE LINES 15-23 .. code-block:: default class Insan: def __init__(self): self.legs = '2 legs' print("__init__ method inside class is being executed!") .. GENERATED FROM PYTHON SOURCE LINES 24-28 .. code-block:: default x = Insan() x.legs .. rst-class:: sphx-glr-script-out .. code-block:: none __init__ method inside class is being executed! '2 legs' .. GENERATED FROM PYTHON SOURCE LINES 29-33 we see that although we only created an instance of class (we call it initialization of an instance as well) and the code inside init method (we don't use the word function for it anymore) got executed. The proper way to add `taruf` method to our Insan class is: .. GENERATED FROM PYTHON SOURCE LINES 35-49 .. code-block:: default class Insan: def __init__(self, name=None, legs='2 legs'): self.name = name self.legs = legs def taruf(self): if self.name: print("Salam, I am " + self.name + ' and I have ' + self.legs) else: print("Salam, I am a Insan but my name has not yed been defined" + ' and I have ' + self.legs) .. GENERATED FROM PYTHON SOURCE LINES 50-54 .. code-block:: default x = Insan() x.taruf() .. rst-class:: sphx-glr-script-out .. code-block:: none Salam, I am a Insan but my name has not yed been defined and I have 2 legs .. GENERATED FROM PYTHON SOURCE LINES 55-59 .. code-block:: default y = Insan("Ali", legs='1 leg') y.taruf() .. rst-class:: sphx-glr-script-out .. code-block:: none Salam, I am Ali and I have 1 leg .. GENERATED FROM PYTHON SOURCE LINES 60-65 Notice how we passed the string ``Ali`` when we were creating class instance and got it transferred to the method ``taruf`` without being explicitly doing it. The method ``taruf`` takes no input arguments (actually it takes one, which it does implicitly) however it prints the value of `name` which we provided to it when we were creating the class instance ``y``. .. GENERATED FROM PYTHON SOURCE LINES 68-69 Let's create a new class instance and use a different name .. GENERATED FROM PYTHON SOURCE LINES 69-74 .. code-block:: default y = Insan("Hasan") y.taruf() # >> Salam, I am Hasan .. rst-class:: sphx-glr-script-out .. code-block:: none Salam, I am Hasan and I have 2 legs .. GENERATED FROM PYTHON SOURCE LINES 75-78 The attribute ``name`` is shared by the whole class and can be accessed anywhere in the class by using the word ``self`` at its start. This is because we initialized it inside ``init`` method. .. GENERATED FROM PYTHON SOURCE LINES 80-83 .. code-block:: default print(y.name) .. rst-class:: sphx-glr-script-out .. code-block:: none Hasan .. GENERATED FROM PYTHON SOURCE LINES 84-87 .. code-block:: default print(y.legs) .. rst-class:: sphx-glr-script-out .. code-block:: none 2 legs .. GENERATED FROM PYTHON SOURCE LINES 88-90 .. code-block:: default print(y.__dict__) .. rst-class:: sphx-glr-script-out .. code-block:: none {'name': 'Hasan', 'legs': '2 legs'} .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.003 seconds) .. _sphx_glr_download_auto_examples_oop_init.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/init.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: init.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: init.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_