.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/oop/class.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_class.py: =================== 3.2 creating class =================== .. GENERATED FROM PYTHON SOURCE LINES 8-13 A class in python can be considered as a collection of functions which share some data (attributes). However, a class need not to have functions in it i.e. it can only consist of attributes. We can define a minimal class as follows .. GENERATED FROM PYTHON SOURCE LINES 15-19 .. code-block:: default class Insan: pass .. GENERATED FROM PYTHON SOURCE LINES 20-24 A ``class`` definition consists of two parts: * header: keyword ``class`` and name of class and then listing of other classes from which this class inherits i.e. superclasses. 3rd argument is optional. * body: indented statements, Above we have only one statement i.e. ``pass``. .. GENERATED FROM PYTHON SOURCE LINES 28-33 Using classes -------------- To use a class, we first have to initiate or instantiate it. This is done by calling the class as if it were a function. For example, we can create an object of ``Insan`` class as follows .. GENERATED FROM PYTHON SOURCE LINES 35-39 .. code-block:: default ali = Insan() husain = Insan() .. GENERATED FROM PYTHON SOURCE LINES 40-43 Above we have created two objects which are called instances of a class. `ali` and `husain` are instances of `Insan` class. If we check the type of these instances we can confirm it. .. GENERATED FROM PYTHON SOURCE LINES 45-48 .. code-block:: default type(ali), type(husain) .. rst-class:: sphx-glr-script-out .. code-block:: none (, ) .. GENERATED FROM PYTHON SOURCE LINES 49-52 .. code-block:: default shabir = husain .. GENERATED FROM PYTHON SOURCE LINES 53-56 .. code-block:: default print(husain == shabir) .. rst-class:: sphx-glr-script-out .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 57-60 .. code-block:: default print(husain == ali) .. rst-class:: sphx-glr-script-out .. code-block:: none False .. GENERATED FROM PYTHON SOURCE LINES 61-63 So `husain` and `ali` are different although they are instances of same class. They are different because they are different instances of the `Insan` class. .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.001 seconds) .. _sphx_glr_download_auto_examples_oop_class.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/class.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: class.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: class.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_