.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/oop/public_vs_private_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_public_vs_private_attributes.py: ================================= 3.7 public vs private attributes ================================= Based on accessibility, attributes can be categorized into 3 categories: * Public * Protected * Private | ** Naming ** | ** Type ** | ** Description ** | | --- | --- | --- | | name | public | The attribute is available both inside and outside class | |_name | protected | should not be should outside class definition | |__name | private | inaccessible and invisible outside class definition | .. GENERATED FROM PYTHON SOURCE LINES 20-34 .. code-block:: default class Insan(): def __init__(self): self.__ghar = "I am private" self._gari = "I am protected" self.name = "I am public" x = Insan() print(x.name) .. rst-class:: sphx-glr-script-out .. code-block:: none I am public .. GENERATED FROM PYTHON SOURCE LINES 35-38 .. code-block:: default print(x._gari) .. rst-class:: sphx-glr-script-out .. code-block:: none I am protected .. GENERATED FROM PYTHON SOURCE LINES 39-43 .. code-block:: default # uncomment following line # x.__ghar # AttributeError .. GENERATED FROM PYTHON SOURCE LINES 44-47 Although the ``Insan`` class do have an attribute anmed ``__priv``, however the error is saying **Insan object has no attribute __priv**. What better privacy can there be? We can also set the values of public attributes. .. GENERATED FROM PYTHON SOURCE LINES 49-54 .. code-block:: default x.name = 'hasan' print(x.name) .. rst-class:: sphx-glr-script-out .. code-block:: none hasan .. GENERATED FROM PYTHON SOURCE LINES 55-57 As said earlier private attribute is not accessible outside the class but we can use them inside class definition as follows: .. GENERATED FROM PYTHON SOURCE LINES 60-75 .. code-block:: default class Insan(): def __init__(self): self.__ghar = "I am private" self._gari = "I am protected" self.name = "I am public" def get_ghar(self): return self.__ghar ali = Insan() ali.get_ghar() .. rst-class:: sphx-glr-script-out .. code-block:: none 'I am private' .. GENERATED FROM PYTHON SOURCE LINES 76-79 name mangling -------------- Every attribute with double underscore will be changed to ``object._class__attribte`` .. GENERATED FROM PYTHON SOURCE LINES 81-84 .. code-block:: default print(ali._Insan__ghar) .. rst-class:: sphx-glr-script-out .. code-block:: none I am private .. GENERATED FROM PYTHON SOURCE LINES 85-86 Ways of accessing and setting attribute values .. GENERATED FROM PYTHON SOURCE LINES 89-130 .. code-block:: default class Insan: def __init__(self, name=None, dob=2000): self.__name = name self.__dob = dob def say_salam(self): if self.__name: print("Salam, I am " + self.__name) else: print("Salam, I am a person without a name") def set_name(self, name): self.__name = name def get_name(self): return self.__name def set_dob(self, by): self.__dob = by def get_dob(self): return self.__dob def __repr__(self): return "Insan('" + self.__name + "', " + str(self.__dob) + ")" def __str__(self): return "Name: " + self.__name + ", born in Year: " + str(self.__dob) x = Insan("Mutahri", 1920) y = Insan("Sadr", 1935) for fard in [x, y]: fard.say_salam() if fard.get_name() == "Sadr": fard.set_dob(1353) print("I was born in the year " + str(fard.get_dob()) + "!") .. rst-class:: sphx-glr-script-out .. code-block:: none Salam, I am Mutahri I was born in the year 1920! Salam, I am Sadr I was born in the year 1353! .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.003 seconds) .. _sphx_glr_download_auto_examples_oop_public_vs_private_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/public_vs_private_attributes.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: public_vs_private_attributes.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: public_vs_private_attributes.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_