.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/oop/str_repr.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_str_repr.py: ========================= 3.6 str and repr methods ========================= .. GENERATED FROM PYTHON SOURCE LINES 8-12 Default behaviour ------------------------------- Unless we overwrite the default method, for most classes the default output does not mean something useful. .. GENERATED FROM PYTHON SOURCE LINES 14-24 .. code-block:: default class Insan(object): pass ali = Insan print(str(ali)) repr(ali) .. rst-class:: sphx-glr-script-out .. code-block:: none "" .. GENERATED FROM PYTHON SOURCE LINES 25-27 If a class does not have explicit definition of ``str`` or ``repr`` methods, python will used the default output. .. GENERATED FROM PYTHON SOURCE LINES 29-35 .. code-block:: default name = 4 str(name) .. rst-class:: sphx-glr-script-out .. code-block:: none '4' .. GENERATED FROM PYTHON SOURCE LINES 36-39 .. code-block:: default repr(name) .. rst-class:: sphx-glr-script-out .. code-block:: none '4' .. GENERATED FROM PYTHON SOURCE LINES 40-43 `name` belongs to class ``int`` and this class has ``str` and `repr` methods so python did not give default output rather used the methods from `int` class. but until here we see no apparent difference. .. GENERATED FROM PYTHON SOURCE LINES 45-48 Overwriting ------------- If we override one of the two methods: .. GENERATED FROM PYTHON SOURCE LINES 51-61 .. code-block:: default class Insan(object): def __str__(object): return "Insan class" ali = Insan() str(ali) .. rst-class:: sphx-glr-script-out .. code-block:: none 'Insan class' .. GENERATED FROM PYTHON SOURCE LINES 62-66 .. code-block:: default repr(ali) .. rst-class:: sphx-glr-script-out .. code-block:: none '<__main__.Insan object at 0x7f0c8cfc1940>' .. GENERATED FROM PYTHON SOURCE LINES 67-77 .. code-block:: default class Insan(object): def __repr__(object): return "Insan class" ali = Insan() str(ali) .. rst-class:: sphx-glr-script-out .. code-block:: none 'Insan class' .. GENERATED FROM PYTHON SOURCE LINES 78-81 .. code-block:: default repr(ali) .. rst-class:: sphx-glr-script-out .. code-block:: none 'Insan class' .. GENERATED FROM PYTHON SOURCE LINES 82-89 So if we overwrite ``repr`` method, ``str`` is also overwritten (second case) but if we only overwrite ``str`` method, ``repr`` will not be overwritten unless we do it explicitly (first case). ``__str__`` is readable and ``__repr__`` is unambiguous Usually the purpose of `str` method to to give a readable output while that of `repr` is give an unambiguous output. consider the following example. .. GENERATED FROM PYTHON SOURCE LINES 91-96 .. code-block:: default name = 'ali' repr(name) .. rst-class:: sphx-glr-script-out .. code-block:: none "'ali'" .. GENERATED FROM PYTHON SOURCE LINES 97-102 .. code-block:: default name2 = eval(repr(name)) print(name2 == name) .. rst-class:: sphx-glr-script-out .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 103-108 .. code-block:: default name3 = eval(str(name)) print(name3 == name) .. rst-class:: sphx-glr-script-out .. code-block:: none False .. GENERATED FROM PYTHON SOURCE LINES 109-113 ``repr`` is such a representation of an object that by evaluating it (by using ``eval``) it will return back the object. ``type(name)`` and ``type(name2)`` will be same. This is why ``name`` and ``name2`` were same. While `str` gives such a representation of an object which is for better reading purpose. .. GENERATED FROM PYTHON SOURCE LINES 115-118 .. code-block:: default type(name), type(name2) .. rst-class:: sphx-glr-script-out .. code-block:: none (, ) .. GENERATED FROM PYTHON SOURCE LINES 119-123 .. code-block:: default type(name3) .. GENERATED FROM PYTHON SOURCE LINES 124-147 .. code-block:: default class Revolution: def __init__(self, name, dob): self.name = name self.dob = dob def __repr__(self): return "Revolution('" + self.name + "', " + str(self.dob) + ")" # def __str__(self): # return "Revolution('" + self.name + "', " + str(self.dob) + ")" x = Revolution("Eslami", 1979) x_str = str(x) print(x_str) print("Type of x_str: ", type(x_str)) new = eval(x_str) print(new) print("Type of new:", type(new)) .. rst-class:: sphx-glr-script-out .. code-block:: none Revolution('Eslami', 1979) Type of x_str: Revolution('Eslami', 1979) Type of new: .. GENERATED FROM PYTHON SOURCE LINES 148-150 So we only overwrote ``repr``, but ``str`` also gave same output as that of ``repr``. Moreover check the types of ``str`` and ``repr``. .. GENERATED FROM PYTHON SOURCE LINES 152-154 In above case we were able to evaluate `str` representation of `name` but in some cases even this may not be possible as in following case: .. GENERATED FROM PYTHON SOURCE LINES 157-177 .. code-block:: default class Revolution: def __init__(self, name, build_year): self.name = name self.build_year = build_year def __repr__(self): return "Revolution('" + self.name + "', " + str(self.build_year) + ")" def __str__(self): return "Name: " + self.name + ", took place in Year: " + str(self.build_year) x = Revolution("Eslami", 1979) x_str = str(x) print(x_str) print("Type of x_str: ", type(x_str)) .. rst-class:: sphx-glr-script-out .. code-block:: none Name: Eslami, took place in Year: 1979 Type of x_str: .. GENERATED FROM PYTHON SOURCE LINES 178-182 .. code-block:: default # uncomment following line # new = eval(x_str) # SyntaxError .. GENERATED FROM PYTHON SOURCE LINES 183-188 .. code-block:: default x_repr = repr(x) new = eval(x_repr) print(new) .. rst-class:: sphx-glr-script-out .. code-block:: none Name: Eslami, took place in Year: 1979 .. GENERATED FROM PYTHON SOURCE LINES 189-190 Consider another example where ``str`` output can not be evaluated .. GENERATED FROM PYTHON SOURCE LINES 192-199 .. code-block:: default import datetime aaj = datetime.datetime.now() aaj_str = str(aaj) print(aaj_str) .. rst-class:: sphx-glr-script-out .. code-block:: none 2024-11-11 07:57:05.232165 .. GENERATED FROM PYTHON SOURCE LINES 200-204 .. code-block:: default aaj_repr = repr(aaj) print(aaj_repr) .. rst-class:: sphx-glr-script-out .. code-block:: none datetime.datetime(2024, 11, 11, 7, 57, 5, 232165) .. GENERATED FROM PYTHON SOURCE LINES 205-209 .. code-block:: default # uncomment following line # eval(aaj_str) # SyntaxError .. GENERATED FROM PYTHON SOURCE LINES 210-213 .. code-block:: default eval(aaj_repr) .. rst-class:: sphx-glr-script-out .. code-block:: none datetime.datetime(2024, 11, 11, 7, 57, 5, 232165) .. GENERATED FROM PYTHON SOURCE LINES 214-216 So ``repr`` is a pure pythonic representation of an object which can be evaluated while ``str`` is for reading purpose. .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.007 seconds) .. _sphx_glr_download_auto_examples_oop_str_repr.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/str_repr.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: str_repr.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: str_repr.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_