|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "2eacaa0c-2999-4b40-bce0-ef2c4d03aeda", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "## Access Modifiers\n", |
| 9 | + "Access Modifiers are the specifications that can be defined to fix boundaries for classes when accessing member functions (methods) or member variables are considered. Various object-oriented languages like C++, Java, Python control access modifications which are used to restrict access to the variables and methods of the class. Most programming languages majorly have the following three forms of access modifiers, which are **Public**, **Private**, and **Protected** in a class." |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "markdown", |
| 14 | + "id": "f97583c2-ccd0-429a-9b5d-0ea9c1db8baa", |
| 15 | + "metadata": {}, |
| 16 | + "source": [ |
| 17 | + "<center><img src=\"https://miro.medium.com/v2/resize:fit:1162/1*AsKRlD4xL50sqSDAOYvhMA.jpeg\" style=\"max-width: 550px; height: auto\"></center>" |
| 18 | + ] |
| 19 | + }, |
| 20 | + { |
| 21 | + "cell_type": "markdown", |
| 22 | + "id": "67285460-1a1c-470c-94ff-25ee77704cff", |
| 23 | + "metadata": {}, |
| 24 | + "source": [ |
| 25 | + "### Public Modifier\n", |
| 26 | + "The members of a class that are declared public are easily accessible from any part of the program. By default, all members (attributes and methods) in a Python class are considered public and can be accessed from anywhere. Consider the given example:" |
| 27 | + ] |
| 28 | + }, |
| 29 | + { |
| 30 | + "cell_type": "code", |
| 31 | + "execution_count": 1, |
| 32 | + "id": "eb24e4d7-7b14-4238-a6b9-a33080a6fee4", |
| 33 | + "metadata": { |
| 34 | + "tags": [] |
| 35 | + }, |
| 36 | + "outputs": [], |
| 37 | + "source": [ |
| 38 | + "# Creating a student class with public member\n", |
| 39 | + "class Pub_Student:\n", |
| 40 | + " name = None # public member by default\n", |
| 41 | + " age = None # public member\n", |
| 42 | + " \n", |
| 43 | + " # constructor\n", |
| 44 | + " def __init__(self, name, age):\n", |
| 45 | + " self.name = name\n", |
| 46 | + " self.age = age\n", |
| 47 | + " \n", |
| 48 | + " def public_method(self):\n", |
| 49 | + " print(\"This is a public method.\")" |
| 50 | + ] |
| 51 | + }, |
| 52 | + { |
| 53 | + "cell_type": "code", |
| 54 | + "execution_count": 2, |
| 55 | + "id": "03b29cbd-f550-47e6-b159-abde41a41f22", |
| 56 | + "metadata": { |
| 57 | + "tags": [] |
| 58 | + }, |
| 59 | + "outputs": [], |
| 60 | + "source": [ |
| 61 | + "# Creating objets/instances from Student class\n", |
| 62 | + "pub_obj = Pub_Student(\"Sohon Roy\", 16)" |
| 63 | + ] |
| 64 | + }, |
| 65 | + { |
| 66 | + "cell_type": "code", |
| 67 | + "execution_count": 3, |
| 68 | + "id": "ad210eac-79b2-44d5-9a93-fba867f0c5ac", |
| 69 | + "metadata": { |
| 70 | + "tags": [] |
| 71 | + }, |
| 72 | + "outputs": [ |
| 73 | + { |
| 74 | + "name": "stdout", |
| 75 | + "output_type": "stream", |
| 76 | + "text": [ |
| 77 | + "Sohon Roy\n", |
| 78 | + "16\n", |
| 79 | + "This is a public method.\n" |
| 80 | + ] |
| 81 | + } |
| 82 | + ], |
| 83 | + "source": [ |
| 84 | + "# Calling the public members of the class\n", |
| 85 | + "print(pub_obj.name)\n", |
| 86 | + "print(pub_obj.age)\n", |
| 87 | + "pub_obj.public_method()" |
| 88 | + ] |
| 89 | + }, |
| 90 | + { |
| 91 | + "cell_type": "markdown", |
| 92 | + "id": "4f6c07f2-0a3e-417d-9630-56bf7225ff40", |
| 93 | + "metadata": {}, |
| 94 | + "source": [ |
| 95 | + "### Protected Modifier\n", |
| 96 | + "The members (attributes and methods) of a class that are declared protected are only accessible to a class derived from it. Data members of a class are declared **protected** by adding a single underscore '_' symbol before the data member of that class. However, this naming convention does not enforce actual access restrictions. It serves more as a hint to other developers that a member should be treated as protected. Here's an example:" |
| 97 | + ] |
| 98 | + }, |
| 99 | + { |
| 100 | + "cell_type": "code", |
| 101 | + "execution_count": 4, |
| 102 | + "id": "8ba0b0bc-9684-4557-9be3-419e7f8a4e7e", |
| 103 | + "metadata": { |
| 104 | + "tags": [] |
| 105 | + }, |
| 106 | + "outputs": [], |
| 107 | + "source": [ |
| 108 | + "# Creating a student class with protected member\n", |
| 109 | + "class Pro_Student:\n", |
| 110 | + " _name = None # protected data member\n", |
| 111 | + " age = None # public data member\n", |
| 112 | + " \n", |
| 113 | + " # constructor\n", |
| 114 | + " def __init__(self, name, age):\n", |
| 115 | + " self._name = name\n", |
| 116 | + " self.age = age\n", |
| 117 | + " \n", |
| 118 | + " def _protected_method(self):\n", |
| 119 | + " print(\"This is a protected method.\")" |
| 120 | + ] |
| 121 | + }, |
| 122 | + { |
| 123 | + "cell_type": "code", |
| 124 | + "execution_count": 5, |
| 125 | + "id": "cb6e666f-e0a7-43cc-bee2-2cbd61f1cb78", |
| 126 | + "metadata": { |
| 127 | + "tags": [] |
| 128 | + }, |
| 129 | + "outputs": [], |
| 130 | + "source": [ |
| 131 | + "# Creating objets/instances from Student class\n", |
| 132 | + "pro_obj = Pro_Student(\"Ayan Ghosh\", 16)" |
| 133 | + ] |
| 134 | + }, |
| 135 | + { |
| 136 | + "cell_type": "code", |
| 137 | + "execution_count": 6, |
| 138 | + "id": "e237234e-a19b-479d-9321-871d78c8d8bc", |
| 139 | + "metadata": { |
| 140 | + "tags": [] |
| 141 | + }, |
| 142 | + "outputs": [ |
| 143 | + { |
| 144 | + "name": "stdout", |
| 145 | + "output_type": "stream", |
| 146 | + "text": [ |
| 147 | + "Ayan Ghosh\n", |
| 148 | + "This is a protected method.\n" |
| 149 | + ] |
| 150 | + } |
| 151 | + ], |
| 152 | + "source": [ |
| 153 | + "# Calling the protected members of the class\n", |
| 154 | + "print(pro_obj._name)\n", |
| 155 | + "pro_obj._protected_method()" |
| 156 | + ] |
| 157 | + }, |
| 158 | + { |
| 159 | + "cell_type": "code", |
| 160 | + "execution_count": 7, |
| 161 | + "id": "a732c9f5-ea3a-466d-8e4b-65c82ba64504", |
| 162 | + "metadata": { |
| 163 | + "tags": [] |
| 164 | + }, |
| 165 | + "outputs": [ |
| 166 | + { |
| 167 | + "name": "stdout", |
| 168 | + "output_type": "stream", |
| 169 | + "text": [ |
| 170 | + "16\n" |
| 171 | + ] |
| 172 | + } |
| 173 | + ], |
| 174 | + "source": [ |
| 175 | + "# Calling the public members of the class\n", |
| 176 | + "print(pro_obj.age)" |
| 177 | + ] |
| 178 | + }, |
| 179 | + { |
| 180 | + "cell_type": "markdown", |
| 181 | + "id": "5734c1b2-865c-4184-a2a2-e32295a94c30", |
| 182 | + "metadata": { |
| 183 | + "tags": [] |
| 184 | + }, |
| 185 | + "source": [ |
| 186 | + "### Private Modifier\n", |
| 187 | + "The member of a class that are declared **private** are accessible within the class only. A private access modifier is the most secure access modifier. Data members of a class are declared private by adding a double underscore '__' symbol before the data member of that class. Here's an example: " |
| 188 | + ] |
| 189 | + }, |
| 190 | + { |
| 191 | + "cell_type": "code", |
| 192 | + "execution_count": 8, |
| 193 | + "id": "b4606abc-52b7-44d8-8788-58481085a453", |
| 194 | + "metadata": { |
| 195 | + "tags": [] |
| 196 | + }, |
| 197 | + "outputs": [], |
| 198 | + "source": [ |
| 199 | + "# Creating a student class with private member\n", |
| 200 | + "class Pri_Student:\n", |
| 201 | + " name = None # public data member\n", |
| 202 | + " __age = None # private data member\n", |
| 203 | + " \n", |
| 204 | + " # constructor\n", |
| 205 | + " def __init__(self, name, age):\n", |
| 206 | + " self.name = name\n", |
| 207 | + " self.__age = age\n", |
| 208 | + " \n", |
| 209 | + " def __private_method(self):\n", |
| 210 | + " print(\"This is a private method.\")" |
| 211 | + ] |
| 212 | + }, |
| 213 | + { |
| 214 | + "cell_type": "code", |
| 215 | + "execution_count": 9, |
| 216 | + "id": "f8238f7d-b0fd-4914-9f01-ae108db8f05a", |
| 217 | + "metadata": { |
| 218 | + "tags": [] |
| 219 | + }, |
| 220 | + "outputs": [], |
| 221 | + "source": [ |
| 222 | + "# Creating objets/instances from Student class\n", |
| 223 | + "pri_obj = Pri_Student(\"Bikash Dey\", 21)" |
| 224 | + ] |
| 225 | + }, |
| 226 | + { |
| 227 | + "cell_type": "code", |
| 228 | + "execution_count": 10, |
| 229 | + "id": "98e60196-7a03-40c0-80bc-5d85aae6fb7b", |
| 230 | + "metadata": { |
| 231 | + "tags": [] |
| 232 | + }, |
| 233 | + "outputs": [ |
| 234 | + { |
| 235 | + "name": "stdout", |
| 236 | + "output_type": "stream", |
| 237 | + "text": [ |
| 238 | + "Bikash Dey\n" |
| 239 | + ] |
| 240 | + } |
| 241 | + ], |
| 242 | + "source": [ |
| 243 | + "# Calling the public members of the class\n", |
| 244 | + "print(pri_obj.name)" |
| 245 | + ] |
| 246 | + }, |
| 247 | + { |
| 248 | + "cell_type": "code", |
| 249 | + "execution_count": 11, |
| 250 | + "id": "8c86cfd2-c646-4749-ad4b-aea37b41fb3c", |
| 251 | + "metadata": { |
| 252 | + "tags": [] |
| 253 | + }, |
| 254 | + "outputs": [], |
| 255 | + "source": [ |
| 256 | + "# Calling the private members of the class\n", |
| 257 | + "#print(pri_obj.__age)" |
| 258 | + ] |
| 259 | + }, |
| 260 | + { |
| 261 | + "cell_type": "markdown", |
| 262 | + "id": "019f3d21-d168-4b58-a151-429bcf007308", |
| 263 | + "metadata": {}, |
| 264 | + "source": [ |
| 265 | + "We will get an **AttributeError** when we try to access the **'__age'** attribute. This is because the **__age** is a private attribute and hence it cannot be accessed from outside the class." |
| 266 | + ] |
| 267 | + }, |
| 268 | + { |
| 269 | + "cell_type": "markdown", |
| 270 | + "id": "9bd09d9b-12f3-4fde-b703-24413ee4da0a", |
| 271 | + "metadata": {}, |
| 272 | + "source": [ |
| 273 | + "**Name Mangling:**<br>\n", |
| 274 | + "Private members in Python are name-mangled, which means their names are modified to make them harder to access. The name-mangling scheme is as follows: <br>a double underscore __ at the beginning of an attribute or method name is replaced with _ClassName, where ClassName is the name of the class." |
| 275 | + ] |
| 276 | + }, |
| 277 | + { |
| 278 | + "cell_type": "code", |
| 279 | + "execution_count": 12, |
| 280 | + "id": "74837de3-e53e-491b-ae47-2f4f8c5e10a7", |
| 281 | + "metadata": { |
| 282 | + "tags": [] |
| 283 | + }, |
| 284 | + "outputs": [ |
| 285 | + { |
| 286 | + "name": "stdout", |
| 287 | + "output_type": "stream", |
| 288 | + "text": [ |
| 289 | + "21\n", |
| 290 | + "This is a private method.\n" |
| 291 | + ] |
| 292 | + } |
| 293 | + ], |
| 294 | + "source": [ |
| 295 | + "# Accessing the private members of the class\n", |
| 296 | + "print(pri_obj._Pri_Student__age)\n", |
| 297 | + "pri_obj._Pri_Student__private_method()" |
| 298 | + ] |
| 299 | + } |
| 300 | + ], |
| 301 | + "metadata": { |
| 302 | + "kernelspec": { |
| 303 | + "display_name": "Python 3 (ipykernel)", |
| 304 | + "language": "python", |
| 305 | + "name": "python3" |
| 306 | + }, |
| 307 | + "language_info": { |
| 308 | + "codemirror_mode": { |
| 309 | + "name": "ipython", |
| 310 | + "version": 3 |
| 311 | + }, |
| 312 | + "file_extension": ".py", |
| 313 | + "mimetype": "text/x-python", |
| 314 | + "name": "python", |
| 315 | + "nbconvert_exporter": "python", |
| 316 | + "pygments_lexer": "ipython3", |
| 317 | + "version": "3.10.9" |
| 318 | + } |
| 319 | + }, |
| 320 | + "nbformat": 4, |
| 321 | + "nbformat_minor": 5 |
| 322 | +} |
0 commit comments