.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/oop/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_attributes.py: =============== 3.3 Attributes =============== .. GENERATED FROM PYTHON SOURCE LINES 8-10 Attribute usually means some specific quality. In python attribute is different than property. .. GENERATED FROM PYTHON SOURCE LINES 13-21 .. code-block:: default class Insan: pass x = Insan() y = Insan() .. GENERATED FROM PYTHON SOURCE LINES 22-24 We have created two objects/instances of class `Insan` namely `x` and `y`. We can bind attributes to class instances as follows: .. GENERATED FROM PYTHON SOURCE LINES 24-28 .. code-block:: default x.name = "Ali" x.dob = "600" .. GENERATED FROM PYTHON SOURCE LINES 29-33 .. code-block:: default y.name = "Hasan" y.dob = "625" .. GENERATED FROM PYTHON SOURCE LINES 34-35 We can now check that the attributes have been associated with `x` and `y`. .. GENERATED FROM PYTHON SOURCE LINES 35-38 .. code-block:: default print(x.name) .. rst-class:: sphx-glr-script-out .. code-block:: none Ali .. GENERATED FROM PYTHON SOURCE LINES 39-42 .. code-block:: default print(y.dob) .. rst-class:: sphx-glr-script-out .. code-block:: none 625 .. GENERATED FROM PYTHON SOURCE LINES 43-48 It should be noticed that we have associated `name` and `dob` attributes to instances of `Insan` class i.e., `x` and `y` and not with `Insan` class. This is a dynamic way of attribute creation. Usually attributes are built inside the class, which we will cover later. what attributes does the instance `x` has? We can find it out as following .. GENERATED FROM PYTHON SOURCE LINES 50-53 .. code-block:: default print(x.__dict__) .. rst-class:: sphx-glr-script-out .. code-block:: none {'name': 'Ali', 'dob': '600'} .. GENERATED FROM PYTHON SOURCE LINES 54-57 .. code-block:: default print(y.__dict__) .. rst-class:: sphx-glr-script-out .. code-block:: none {'name': 'Hasan', 'dob': '625'} .. GENERATED FROM PYTHON SOURCE LINES 58-59 We can also bind attributes to class names as well. .. GENERATED FROM PYTHON SOURCE LINES 59-68 .. code-block:: default class Insan(object): pass x = Insan() Insan.cast = "Jat" .. GENERATED FROM PYTHON SOURCE LINES 69-70 attribute `cast` is currently associated with instance `x`. .. GENERATED FROM PYTHON SOURCE LINES 70-73 .. code-block:: default print(x.cast) .. rst-class:: sphx-glr-script-out .. code-block:: none Jat .. GENERATED FROM PYTHON SOURCE LINES 74-75 We can change the attribute `cast`` associated with instance `x` as below .. GENERATED FROM PYTHON SOURCE LINES 75-78 .. code-block:: default x.cast = "cheema" .. GENERATED FROM PYTHON SOURCE LINES 79-82 .. code-block:: default print(x.cast) .. rst-class:: sphx-glr-script-out .. code-block:: none cheema .. GENERATED FROM PYTHON SOURCE LINES 83-84 what is attribute `cast` associated with class name 'Insan'? .. GENERATED FROM PYTHON SOURCE LINES 84-87 .. code-block:: default print(Insan.cast) # >> Jat .. rst-class:: sphx-glr-script-out .. code-block:: none Jat .. GENERATED FROM PYTHON SOURCE LINES 88-92 .. code-block:: default y = Insan() print(y.cast) # what is attribute `cast` associated with instance `y`? .. rst-class:: sphx-glr-script-out .. code-block:: none Jat .. GENERATED FROM PYTHON SOURCE LINES 93-96 `y` was never assigned an attributed named `cast`. Still it threw a value, why? Let's make the attribute `cast` associated with `Insan` as `Insaniyat` now .. GENERATED FROM PYTHON SOURCE LINES 98-103 .. code-block:: default Insan.cast = "insaniyat" print('y_cast: ', y.cast) print('x_cast: ', x.cast) .. rst-class:: sphx-glr-script-out .. code-block:: none y_cast: insaniyat x_cast: cheema .. GENERATED FROM PYTHON SOURCE LINES 104-107 .. code-block:: default print(x.__dict__) .. rst-class:: sphx-glr-script-out .. code-block:: none {'cast': 'cheema'} .. GENERATED FROM PYTHON SOURCE LINES 108-111 .. code-block:: default print(y.__dict__) .. rst-class:: sphx-glr-script-out .. code-block:: none {} .. GENERATED FROM PYTHON SOURCE LINES 112-114 empty so if we call the attribute `cast` for instance `y`, python will first look into y attributes and if it does not find then it will look into attributes of `Insan` .. GENERATED FROM PYTHON SOURCE LINES 116-120 .. code-block:: default # mappingproxy({'__module__': '__main__', '__weakref__': , '__doc__': None, '__dict__': , 'cast': 'insaniyat'}) print(Insan.__dict__) .. rst-class:: sphx-glr-script-out .. code-block:: none {'__module__': '__main__', '__dict__': , '__weakref__': , '__doc__': None, 'cast': 'insaniyat'} .. GENERATED FROM PYTHON SOURCE LINES 121-125 So even though `y` instance itself does not have an attribute named `cast` so it checked whether the attribute `cast` exists in attributes of class `Insan`? If yes (which is the case) so `y` gets the attribute from `Insan` while `x` already had attribute named `cast` so it did not get attribute from class `Insan`. .. GENERATED FROM PYTHON SOURCE LINES 127-128 If we try to get an attribute which is non-existing, we will get an ``AttributeError`` .. GENERATED FROM PYTHON SOURCE LINES 130-134 .. code-block:: default # uncomment following line # x.age # >> AttributeError: `Insan` object has no attribute `age` .. GENERATED FROM PYTHON SOURCE LINES 135-136 One way to prevent such error is to provide a default value for the attribute by .. GENERATED FROM PYTHON SOURCE LINES 138-141 .. code-block:: default getattr(x, 'age', 90) .. rst-class:: sphx-glr-script-out .. code-block:: none 90 .. GENERATED FROM PYTHON SOURCE LINES 142-143 We can bind attributes to function names similarly .. GENERATED FROM PYTHON SOURCE LINES 146-155 .. code-block:: default def chor(name): return name + ' chor hai' chor.age = 61 print(chor.age) .. rst-class:: sphx-glr-script-out .. code-block:: none 61 .. GENERATED FROM PYTHON SOURCE LINES 156-157 We can use this trick to count number of function calls .. GENERATED FROM PYTHON SOURCE LINES 159-169 .. code-block:: default def chor(name): chor.no_of_calls = getattr(chor, "no_of_calls", 0) + 1 return name for i in range(10): chor('nawaz') .. GENERATED FROM PYTHON SOURCE LINES 170-173 .. code-block:: default print(chor.no_of_calls) .. rst-class:: sphx-glr-script-out .. code-block:: none 10 .. GENERATED FROM PYTHON SOURCE LINES 174-176 To properly create class instances we need to define `methods` in the class body, which we will learn next .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.006 seconds) .. _sphx_glr_download_auto_examples_oop_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/attributes.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: attributes.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: attributes.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_