.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/oop/getattr_setattr.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_getattr_setattr.py: ======================== 3.17 getattr vs setattr ======================== This lesson shows the usage of ``setattr`` and ``getattr`` .. GENERATED FROM PYTHON SOURCE LINES 10-13 The ``getattr`` and ``setattr`` are builtin functions which are used to get and set an attribute to a python object. Following examples show how to use these functions to set and get attributes of a class. .. GENERATED FROM PYTHON SOURCE LINES 15-17 setattr -------- .. GENERATED FROM PYTHON SOURCE LINES 17-31 .. code-block:: default class Human: def __init__(self, name): self.name = name def grow(self): setattr(self, 'empathy', 10) return human = Human("Ali") .. GENERATED FROM PYTHON SOURCE LINES 32-33 When we created the instance of `Human` class, it did not have `empathy` attribute. .. GENERATED FROM PYTHON SOURCE LINES 33-37 .. code-block:: default # uncomment following line # human.empathy # -> AttributeError: 'Human' object has no attribute 'empathy' .. GENERATED FROM PYTHON SOURCE LINES 38-40 After executing the `grow` method of `Human` class, the `empathy` attribute is set to it using ``setattr`` function. .. GENERATED FROM PYTHON SOURCE LINES 40-45 .. code-block:: default human.grow() print(human.empathy) .. rst-class:: sphx-glr-script-out .. code-block:: none 10 .. GENERATED FROM PYTHON SOURCE LINES 46-51 The first argument to ``setattr`` is the object to which we want to set the attribute. The second argument is the name of attribute and the third argument is the value of the attribute. If we want to set/change the value of `empathy` attribute of `human` to 14, we can do this using ``setattr`` as below. .. GENERATED FROM PYTHON SOURCE LINES 51-56 .. code-block:: default setattr(human, "empathy", 14) print(human.empathy) .. rst-class:: sphx-glr-script-out .. code-block:: none 14 .. GENERATED FROM PYTHON SOURCE LINES 57-59 It will be obvious from above examples that the function ``setattr`` can be used both inside the class and outside the class definition. .. GENERATED FROM PYTHON SOURCE LINES 59-64 .. code-block:: default human.empathy = 100 print(human.empathy) .. rst-class:: sphx-glr-script-out .. code-block:: none 100 .. GENERATED FROM PYTHON SOURCE LINES 65-68 From above example we can infer that doing `human.empathy = 10` is similar to `setattr(human, 'empathy', 100)`. This can be translated as, setting the attribute of `human` with the name `empathy` to 100. .. GENERATED FROM PYTHON SOURCE LINES 70-73 getattr --------- ``getattr`` is opposite of ``setattr``. It is used to fetch the attribute value of an object. .. GENERATED FROM PYTHON SOURCE LINES 73-76 .. code-block:: default print(getattr(human, 'empathy')) .. rst-class:: sphx-glr-script-out .. code-block:: none 100 .. GENERATED FROM PYTHON SOURCE LINES 77-79 In other words, doing `human.empathy` is similar to running `getattr(human, 'empathy')` The second argument to both ``setattr`` and ``getattr`` is string (``str``) type. .. GENERATED FROM PYTHON SOURCE LINES 79-97 .. code-block:: default class Human: def __init__(self, name): self.name = name def grow(self): setattr(self, 'empathy', 10) return def info(self): empathy = getattr(self, 'empathy', None) return empathy human = Human("Ali") .. GENERATED FROM PYTHON SOURCE LINES 98-99 But what if the object does not have the attribute that we are trying to fetch? .. GENERATED FROM PYTHON SOURCE LINES 99-103 .. code-block:: default # uncomment following line # human.empathy # -> AttributeError: 'Human' object has no attribute 'empathy' .. GENERATED FROM PYTHON SOURCE LINES 104-106 Running the above code will give us AttributeError because, the `human` does not yet have `empathy` attribute. .. GENERATED FROM PYTHON SOURCE LINES 108-111 .. code-block:: default human.info() .. GENERATED FROM PYTHON SOURCE LINES 112-120 But why above cell did not throw AttributeError, even though we are getting, `empathy` attribute in it? This is because the 3rd argument in ``getattr`` function in `info` method is ``None``. The 3rd argument is the default value of the attribute which we are trying to fetch. This means, when the object `human` did not have `empathy` attribute and we tried to get it using ``getattr``, the default value ``None`` was returned. We can verify this by printing the output of `human.info()`. .. GENERATED FROM PYTHON SOURCE LINES 120-123 .. code-block:: default print(human.info()) .. rst-class:: sphx-glr-script-out .. code-block:: none None .. GENERATED FROM PYTHON SOURCE LINES 124-126 However, if we run the `grow` method first, this will result in setting the `empathy` attribute to `human`. Consequently, we can see a different output when we run `human.info` after that. .. GENERATED FROM PYTHON SOURCE LINES 126-131 .. code-block:: default human.grow() print(human.info()) .. rst-class:: sphx-glr-script-out .. code-block:: none 10 .. GENERATED FROM PYTHON SOURCE LINES 132-145 **Question:** What will be the output of following code? .. code-block:: python class Man: def __init__(self, weight, height): self.bmi = weight / height**2 ali = Man(77, 1.71) bmi = getattr(ali, 'bmi') setattr(ali, 'bmi', bmi - 1.33288875) print(ali.bmi) .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.003 seconds) .. _sphx_glr_download_auto_examples_oop_getattr_setattr.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/getattr_setattr.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: getattr_setattr.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: getattr_setattr.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_