{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "1527de64", "metadata": {}, "outputs": [], "source": [ "class A:\n", " _a=10 #private\n", " __b=20 #Protected\n", " def show(self):\n", " print(\"a= \", self._a)\n", " print(\"b= \", self.__b)" ] }, { "cell_type": "code", "execution_count": 2, "id": "0152a7de", "metadata": {}, "outputs": [], "source": [ "obj=A()" ] }, { "cell_type": "code", "execution_count": 6, "id": "3d64114a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a= 10\n", "b= 20\n" ] } ], "source": [ "obj.show()" ] }, { "cell_type": "code", "execution_count": 10, "id": "67aa7f84", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Protected Object calling outside of class 10\n" ] } ], "source": [ "print(f\"Protected Object calling outside of class {obj._a}\")" ] }, { "cell_type": "code", "execution_count": 11, "id": "605135d5", "metadata": {}, "outputs": [ { "ename": "AttributeError", "evalue": "'A' object has no attribute '__b'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", "Input \u001b[1;32mIn [11]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m private Object calling outside of class \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mobj\u001b[38;5;241m.\u001b[39m__b\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n", "\u001b[1;31mAttributeError\u001b[0m: 'A' object has no attribute '__b'" ] } ], "source": [ "print(f\" private Object calling outside of class {obj.__b}\")" ] }, { "cell_type": "code", "execution_count": null, "id": "902819a6", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.12" } }, "nbformat": 4, "nbformat_minor": 5 }