You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: .ipynb_checkpoints/OOPS-checkpoint.ipynb
+81-17Lines changed: 81 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -32,7 +32,10 @@
32
32
"Using `class` keyword.\n",
33
33
"Name of a class should start with an uppercase letter.\n",
34
34
"\n",
35
-
"Syntax: `class Class_name`"
35
+
"Syntax: \n",
36
+
"```python\n",
37
+
"class Class_name\n",
38
+
"```"
36
39
]
37
40
},
38
41
{
@@ -54,7 +57,10 @@
54
57
"\n",
55
58
"It is an instance of a class.\n",
56
59
"\n",
57
-
"Syntax: `obj_name = Class_name()`"
60
+
"Syntax: \n",
61
+
"```python\n",
62
+
"obj_name = Class_name()\n",
63
+
"```"
58
64
]
59
65
},
60
66
{
@@ -96,21 +102,79 @@
96
102
"\n",
97
103
"Calls the attributes for current instance or object.\n",
98
104
"\n",
99
-
"Syntax: `self.attribute_name`"
105
+
"Syntax: \n",
106
+
"```python\n",
107
+
"self.attribute_name\n",
108
+
"```"
100
109
]
101
110
},
102
111
{
103
112
"cell_type": "markdown",
104
113
"metadata": {},
105
114
"source": [
106
-
"## Defining attributes in a class:\n",
115
+
"## Defining attributes in a class\n",
107
116
"\n",
108
-
"Attributes or Properties are defined using a constructor.\n",
117
+
"Attributes or Properties are generally of two types:\n",
118
+
"1. Fields (variables)\n",
119
+
"2. Methods (functions)\n",
109
120
"\n",
121
+
"### Fields\n",
122
+
"The `fields` are the variables or containers that store data used and related to an object/Class\n",
123
+
"\n",
124
+
"Types: \n",
125
+
"1. Instance fields\n",
126
+
"2. Class or static fields\n"
127
+
]
128
+
},
129
+
{
130
+
"cell_type": "markdown",
131
+
"metadata": {},
132
+
"source": [
133
+
"The `methods` are block of code that, like functions, execute a specific task\n",
134
+
"\n",
135
+
"Though a method and function are defined in the same way, they have differences.\n",
136
+
"\n",
137
+
"A **function** is a piece of code that is called by name. It can be passed data to operate on (i.e. the parameters) and can optionally return data (the return value). All data that is passed to a function is explicitly passed.\n",
138
+
"\n",
139
+
"A **method** is a piece of code that is called by a name that is associated with an object. \n",
140
+
"\n",
141
+
"In most respects it is identical to a function except for two key differences:\n",
142
+
"\n",
143
+
"1. A method is implicitly passed the object on which it was called.\n",
144
+
"2. A method is able to operate on data that is contained within the class \n",
145
+
"\n",
146
+
"(remembering that an object is an instance of a class - the class is the definition, the object is an instance of that data).\n",
147
+
"\n",
148
+
"Types:\n",
149
+
"1. Instance methods\n",
150
+
"2. Static methods"
151
+
]
152
+
},
153
+
{
154
+
"cell_type": "markdown",
155
+
"metadata": {},
156
+
"source": [
110
157
"### Constructor:\n",
111
-
"Executes a set of code whenever a new object/instance is created.\n",
158
+
"A method that executes a set of code whenever a new object/instance is created.\n",
159
+
"\n",
160
+
"Defined as `__new__()`. Generally, this is not defined/overridden and follows the default definition as in the `object` class\n",
161
+
"\n",
162
+
"```python\n",
163
+
"def __new__(cls, *args, **kwargs):\n",
164
+
" # Custom constructor\n",
165
+
"```\n",
112
166
"\n",
113
-
"Defined by \\_\\_init\\_\\_ method."
167
+
"### Initializer:\n",
168
+
"A method that initializes the object (instance) created by call with the parameters passed. \n",
169
+
"\n",
170
+
"The `__new__()` method calls this automatically.\n",
171
+
"\n",
172
+
"Defined as `__init__()`.\n",
173
+
"\n",
174
+
"```python\n",
175
+
"def __init__(self, *args, **kwargs):\n",
176
+
" # Statements for object Initialization\n",
177
+
"```"
114
178
]
115
179
},
116
180
{
@@ -189,9 +253,14 @@
189
253
"metadata": {},
190
254
"source": [
191
255
"## destructor:\n",
192
-
"Delete the current instance of class or object.\n",
256
+
"Delete the current instance of class or object. It has default definition in the object Base class\n",
257
+
"\n",
258
+
"Use `__del__()` method to override it\n",
193
259
"\n",
194
-
"Use \\_\\_del\\_\\_ method"
260
+
"```python\n",
261
+
"def __del__(self, *args, **kwargs):\n",
262
+
" # Custom destructor\n",
263
+
"```"
195
264
]
196
265
},
197
266
{
@@ -263,15 +332,10 @@
263
332
"cell_type": "markdown",
264
333
"metadata": {},
265
334
"source": [
266
-
"Check the complete example [OOPS.py](OOPS.py) "
335
+
"Check the complete example [OOPS.py](OOPS.py) \n",
336
+
"\n",
337
+
"Ignore the imports. They have been used to provide hinting about the types of each variable"
Copy file name to clipboardExpand all lines: OOPS.ipynb
+79-10Lines changed: 79 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -32,7 +32,10 @@
32
32
"Using `class` keyword.\n",
33
33
"Name of a class should start with an uppercase letter.\n",
34
34
"\n",
35
-
"Syntax: `class Class_name`"
35
+
"Syntax: \n",
36
+
"```python\n",
37
+
"class Class_name\n",
38
+
"```"
36
39
]
37
40
},
38
41
{
@@ -54,7 +57,10 @@
54
57
"\n",
55
58
"It is an instance of a class.\n",
56
59
"\n",
57
-
"Syntax: `obj_name = Class_name()`"
60
+
"Syntax: \n",
61
+
"```python\n",
62
+
"obj_name = Class_name()\n",
63
+
"```"
58
64
]
59
65
},
60
66
{
@@ -96,21 +102,79 @@
96
102
"\n",
97
103
"Calls the attributes for current instance or object.\n",
98
104
"\n",
99
-
"Syntax: `self.attribute_name`"
105
+
"Syntax: \n",
106
+
"```python\n",
107
+
"self.attribute_name\n",
108
+
"```"
100
109
]
101
110
},
102
111
{
103
112
"cell_type": "markdown",
104
113
"metadata": {},
105
114
"source": [
106
-
"## Defining attributes in a class:\n",
115
+
"## Defining attributes in a class\n",
107
116
"\n",
108
-
"Attributes or Properties are defined using a constructor.\n",
117
+
"Attributes or Properties are generally of two types:\n",
118
+
"1. Fields (variables)\n",
119
+
"2. Methods (functions)\n",
109
120
"\n",
121
+
"### Fields\n",
122
+
"The `fields` are the variables or containers that store data used and related to an object/Class\n",
123
+
"\n",
124
+
"Types: \n",
125
+
"1. Instance fields\n",
126
+
"2. Class or static fields\n"
127
+
]
128
+
},
129
+
{
130
+
"cell_type": "markdown",
131
+
"metadata": {},
132
+
"source": [
133
+
"The `methods` are block of code that, like functions, execute a specific task\n",
134
+
"\n",
135
+
"Though a method and function are defined in the same way, they have differences.\n",
136
+
"\n",
137
+
"A **function** is a piece of code that is called by name. It can be passed data to operate on (i.e. the parameters) and can optionally return data (the return value). All data that is passed to a function is explicitly passed.\n",
138
+
"\n",
139
+
"A **method** is a piece of code that is called by a name that is associated with an object. \n",
140
+
"\n",
141
+
"In most respects it is identical to a function except for two key differences:\n",
142
+
"\n",
143
+
"1. A method is implicitly passed the object on which it was called.\n",
144
+
"2. A method is able to operate on data that is contained within the class \n",
145
+
"\n",
146
+
"(remembering that an object is an instance of a class - the class is the definition, the object is an instance of that data).\n",
147
+
"\n",
148
+
"Types:\n",
149
+
"1. Instance methods\n",
150
+
"2. Static methods"
151
+
]
152
+
},
153
+
{
154
+
"cell_type": "markdown",
155
+
"metadata": {},
156
+
"source": [
110
157
"### Constructor:\n",
111
-
"Executes a set of code whenever a new object/instance is created.\n",
158
+
"A method that executes a set of code whenever a new object/instance is created.\n",
112
159
"\n",
113
-
"Defined by \\_\\_init\\_\\_ method."
160
+
"Defined as `__new__()`. Generally, this is not defined/overridden and follows the default definition as in the `object` class\n",
161
+
"\n",
162
+
"```python\n",
163
+
"def __new__(cls, *args, **kwargs):\n",
164
+
" # Custom constructor\n",
165
+
"```\n",
166
+
"\n",
167
+
"### Initializer:\n",
168
+
"A method that initializes the object (instance) created by call with the parameters passed. \n",
169
+
"\n",
170
+
"The `__new__()` method calls this automatically.\n",
171
+
"\n",
172
+
"Defined as `__init__()`.\n",
173
+
"\n",
174
+
"```python\n",
175
+
"def __init__(self, *args, **kwargs):\n",
176
+
" # Statements for object Initialization\n",
177
+
"```"
114
178
]
115
179
},
116
180
{
@@ -189,9 +253,14 @@
189
253
"metadata": {},
190
254
"source": [
191
255
"## destructor:\n",
192
-
"Delete the current instance of class or object.\n",
256
+
"Delete the current instance of class or object. It has default definition in the object Base class\n",
257
+
"\n",
258
+
"Use `__del__()` method to override it\n",
193
259
"\n",
194
-
"Use \\_\\_del\\_\\_ method"
260
+
"```python\n",
261
+
"def __del__(self, *args, **kwargs):\n",
262
+
" # Custom destructor\n",
263
+
"```"
195
264
]
196
265
},
197
266
{
@@ -265,7 +334,7 @@
265
334
"source": [
266
335
"Check the complete example [OOPS.py](OOPS.py) \n",
267
336
"\n",
268
-
"Ignore the imports. They have used to provide hinting about the types of each variable"
337
+
"Ignore the imports. They have been used to provide hinting about the types of each variable"
<h2 id="Class:">Class:<a class="anchor-link" href="#Class:">¶</a></h2><p>It is a structure or a building block (a code block) representing or defining the attributes (features and behaviour) of similar parameters.</p>
<h2 id="self-Keyword:"><em>self</em> Keyword:<a class="anchor-link" href="#self-Keyword:">¶</a></h2><p>Calls the attributes for current instance or object.</p>
<h2 id="Defining-attributes-in-a-class">Defining attributes in a class<a class="anchor-link" href="#Defining-attributes-in-a-class">¶</a></h2><p>Attributes or Properties are generally of two types:</p>
14378
+
<ol>
14379
+
<li>Fields (variables)</li>
14380
+
<li>Methods (functions)</li>
14381
+
</ol>
14382
+
<h3 id="Fields">Fields<a class="anchor-link" href="#Fields">¶</a></h3><p>The <code>fields</code> are the variables or containers that store data used and related to an object/Class</p>
<h2 id="Defining-attributes-in-a-class:">Defining attributes in a class:<a class="anchor-link" href="#Defining-attributes-in-a-class:">¶</a></h2><p>Attributes or Properties are defined using a constructor.</p>
14372
-
<h3 id="Constructor:">Constructor:<a class="anchor-link" href="#Constructor:">¶</a></h3><p>Executes a set of code whenever a new object/instance is created.</p>
14373
-
<p>Defined by __init__ method.</p>
14393
+
<p>The <code>methods</code> are block of code that, like functions, execute a specific task</p>
14394
+
<p>Though a method and function are defined in the same way, they have differences.</p>
14395
+
<p>A <strong>function</strong> is a piece of code that is called by name. It can be passed data to operate on (i.e. the parameters) and can optionally return data (the return value). All data that is passed to a function is explicitly passed.</p>
14396
+
<p>A <strong>method</strong> is a piece of code that is called by a name that is associated with an object.</p>
14397
+
<p>In most respects it is identical to a function except for two key differences:</p>
14398
+
<ol>
14399
+
<li>A method is implicitly passed the object on which it was called.</li>
14400
+
<li>A method is able to operate on data that is contained within the class </li>
14401
+
</ol>
14402
+
<p>(remembering that an object is an instance of a class - the class is the definition, the object is an instance of that data).</p>
<h3 id="Constructor:">Constructor:<a class="anchor-link" href="#Constructor:">¶</a></h3><p>A method that executes a set of code whenever a new object/instance is created.</p>
14414
+
<p>Defined as <code>__new__()</code>. Generally, this is not defined/overridden and follows the default definition as in the <code>object</code> class</p>
<h3 id="Initializer:">Initializer:<a class="anchor-link" href="#Initializer:">¶</a></h3><p>A method that initializes the object (instance) created by call with the parameters passed.</p>
14419
+
<p>The <code>__new__()</code> method calls this automatically.</p>
<h2 id="destructor:">destructor:<a class="anchor-link" href="#destructor:">¶</a></h2><p>Delete the current instance of class or object.</p>
14497
-
<p>Use __del__ method</p>
14546
+
<h2 id="destructor:">destructor:<a class="anchor-link" href="#destructor:">¶</a></h2><p>Delete the current instance of class or object. It has default definition in the object Base class</p>
14547
+
<p>Use <code>__del__()</code> method to override it</p>
0 commit comments