.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/oop/class_vs_instance_attributes.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_vs_instance_attributes.py: ================================== 3.8 class vs instance attributes ================================== .. GENERATED FROM PYTHON SOURCE LINES 6-17 .. code-block:: default class Insan: ethnicity = "Balochi" x = Insan() y = Insan() print(x.ethnicity) .. rst-class:: sphx-glr-script-out .. code-block:: none Balochi .. GENERATED FROM PYTHON SOURCE LINES 18-21 .. code-block:: default print(y.ethnicity) .. rst-class:: sphx-glr-script-out .. code-block:: none Balochi .. GENERATED FROM PYTHON SOURCE LINES 22-25 .. code-block:: default print(Insan.ethnicity) .. rst-class:: sphx-glr-script-out .. code-block:: none Balochi .. GENERATED FROM PYTHON SOURCE LINES 26-30 .. code-block:: default x.ethnicity = "muhajir" y.ethnicity .. rst-class:: sphx-glr-script-out .. code-block:: none 'Balochi' .. GENERATED FROM PYTHON SOURCE LINES 31-34 .. code-block:: default Insan.ethnicity .. rst-class:: sphx-glr-script-out .. code-block:: none 'Balochi' .. GENERATED FROM PYTHON SOURCE LINES 35-39 .. code-block:: default Insan.ethnicity = "Insaniyat" Insan.ethnicity .. rst-class:: sphx-glr-script-out .. code-block:: none 'Insaniyat' .. GENERATED FROM PYTHON SOURCE LINES 40-43 .. code-block:: default y.ethnicity .. rst-class:: sphx-glr-script-out .. code-block:: none 'Insaniyat' .. GENERATED FROM PYTHON SOURCE LINES 44-47 .. code-block:: default x.ethnicity .. rst-class:: sphx-glr-script-out .. code-block:: none 'muhajir' .. GENERATED FROM PYTHON SOURCE LINES 48-50 Because the attribute was changed for class and instance ``x`` was created before this change so ``x`` still has old attribute value. .. GENERATED FROM PYTHON SOURCE LINES 52-55 .. code-block:: default x.__dict__ .. rst-class:: sphx-glr-script-out .. code-block:: none {'ethnicity': 'muhajir'} .. GENERATED FROM PYTHON SOURCE LINES 56-59 .. code-block:: default print(y.__dict__) .. rst-class:: sphx-glr-script-out .. code-block:: none {} .. GENERATED FROM PYTHON SOURCE LINES 60-62 Because `y` itself does not have attribute ``ethnicity`` so it looked for the attribute value in class `Insan` and used that value for itself. .. GENERATED FROM PYTHON SOURCE LINES 64-67 .. code-block:: default print(Insan.__dict__) .. rst-class:: sphx-glr-script-out .. code-block:: none {'__module__': '__main__', 'ethnicity': 'Insaniyat', '__dict__': , '__weakref__': , '__doc__': None} .. GENERATED FROM PYTHON SOURCE LINES 68-70 So class attributes and instance attributes can have different values. We can find out attributes of ``class`` of an ``instance`` from the `instance` using following code .. GENERATED FROM PYTHON SOURCE LINES 72-76 .. code-block:: default print(x.__class__.__dict__) .. rst-class:: sphx-glr-script-out .. code-block:: none {'__module__': '__main__', 'ethnicity': 'Insaniyat', '__dict__': , '__weakref__': , '__doc__': None} .. GENERATED FROM PYTHON SOURCE LINES 77-93 .. code-block:: default class philosopher: Quotes = ( """What cannot be imagined cannot even be talked about.""", """Philosophy is a battle against the bewitchment of our intelligence by means of language.,""", """The limits of my language means the limits of my world.""" ) def __init__(self, name='Wittgenstein', dob=1889): self.name = name self.build_year = build_year # other methods as usual .. GENERATED FROM PYTHON SOURCE LINES 94-99 .. code-block:: default for number, quote in enumerate(philosopher.Quotes): print(str(number + 1) + ":\n" + quote) .. rst-class:: sphx-glr-script-out .. code-block:: none 1: What cannot be imagined cannot even be talked about. 2: Philosophy is a battle against the bewitchment of our intelligence by means of language., 3: The limits of my language means the limits of my world. .. GENERATED FROM PYTHON SOURCE LINES 100-102 Counting instances of a class ------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 104-126 .. code-block:: default class Insan: counter = 0 def __init__(self): # every time a new instance is created the attribute 'counter' of 'Insan' increases by 1 type(self).counter += 1 def __del__(self): # every time an instance of `Insan` is deleted, the attribute 'counter' of 'Insan' decreases by 1 type(self).counter -= 1 x = Insan() print("Population count is: : " + str(Insan.counter)) y = Insan() print("Population count is: : " + str(Insan.counter)) del x print("Population count is: : " + str(Insan.counter)) del y print("Population count is: : " + str(Insan.counter)) .. rst-class:: sphx-glr-script-out .. code-block:: none Population count is: : 1 Population count is: : 2 Population count is: : 1 Population count is: : 0 .. GENERATED FROM PYTHON SOURCE LINES 127-128 ``type(self)`` is evaluated back to `Insan` .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.005 seconds) .. _sphx_glr_download_auto_examples_oop_class_vs_instance_attributes.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_vs_instance_attributes.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: class_vs_instance_attributes.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: class_vs_instance_attributes.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_