Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7ee28a5

Browse files
Added content on fields and methods
1 parent 3f48bca commit 7ee28a5

File tree

3 files changed

+222
-36
lines changed

3 files changed

+222
-36
lines changed

‎.ipynb_checkpoints/OOPS-checkpoint.ipynb

Lines changed: 81 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@
3232
"Using `class` keyword.\n",
3333
"Name of a class should start with an uppercase letter.\n",
3434
"\n",
35-
"Syntax: `class Class_name`"
35+
"Syntax: \n",
36+
"```python\n",
37+
"class Class_name\n",
38+
"```"
3639
]
3740
},
3841
{
@@ -54,7 +57,10 @@
5457
"\n",
5558
"It is an instance of a class.\n",
5659
"\n",
57-
"Syntax: `obj_name = Class_name()`"
60+
"Syntax: \n",
61+
"```python\n",
62+
"obj_name = Class_name()\n",
63+
"```"
5864
]
5965
},
6066
{
@@ -96,21 +102,79 @@
96102
"\n",
97103
"Calls the attributes for current instance or object.\n",
98104
"\n",
99-
"Syntax: `self.attribute_name`"
105+
"Syntax: \n",
106+
"```python\n",
107+
"self.attribute_name\n",
108+
"```"
100109
]
101110
},
102111
{
103112
"cell_type": "markdown",
104113
"metadata": {},
105114
"source": [
106-
"## Defining attributes in a class:\n",
115+
"## Defining attributes in a class\n",
107116
"\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",
109120
"\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": [
110157
"### 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",
112166
"\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+
"```"
114178
]
115179
},
116180
{
@@ -189,9 +253,14 @@
189253
"metadata": {},
190254
"source": [
191255
"## 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",
193259
"\n",
194-
"Use \\_\\_del\\_\\_ method"
260+
"```python\n",
261+
"def __del__(self, *args, **kwargs):\n",
262+
" # Custom destructor\n",
263+
"```"
195264
]
196265
},
197266
{
@@ -263,15 +332,10 @@
263332
"cell_type": "markdown",
264333
"metadata": {},
265334
"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"
267338
]
268-
},
269-
{
270-
"cell_type": "code",
271-
"execution_count": null,
272-
"metadata": {},
273-
"outputs": [],
274-
"source": []
275339
}
276340
],
277341
"metadata": {

‎OOPS.ipynb

Lines changed: 79 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@
3232
"Using `class` keyword.\n",
3333
"Name of a class should start with an uppercase letter.\n",
3434
"\n",
35-
"Syntax: `class Class_name`"
35+
"Syntax: \n",
36+
"```python\n",
37+
"class Class_name\n",
38+
"```"
3639
]
3740
},
3841
{
@@ -54,7 +57,10 @@
5457
"\n",
5558
"It is an instance of a class.\n",
5659
"\n",
57-
"Syntax: `obj_name = Class_name()`"
60+
"Syntax: \n",
61+
"```python\n",
62+
"obj_name = Class_name()\n",
63+
"```"
5864
]
5965
},
6066
{
@@ -96,21 +102,79 @@
96102
"\n",
97103
"Calls the attributes for current instance or object.\n",
98104
"\n",
99-
"Syntax: `self.attribute_name`"
105+
"Syntax: \n",
106+
"```python\n",
107+
"self.attribute_name\n",
108+
"```"
100109
]
101110
},
102111
{
103112
"cell_type": "markdown",
104113
"metadata": {},
105114
"source": [
106-
"## Defining attributes in a class:\n",
115+
"## Defining attributes in a class\n",
107116
"\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",
109120
"\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": [
110157
"### 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",
112159
"\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+
"```"
114178
]
115179
},
116180
{
@@ -189,9 +253,14 @@
189253
"metadata": {},
190254
"source": [
191255
"## 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",
193259
"\n",
194-
"Use \\_\\_del\\_\\_ method"
260+
"```python\n",
261+
"def __del__(self, *args, **kwargs):\n",
262+
" # Custom destructor\n",
263+
"```"
195264
]
196265
},
197266
{
@@ -265,7 +334,7 @@
265334
"source": [
266335
"Check the complete example [OOPS.py](OOPS.py) \n",
267336
"\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"
269338
]
270339
}
271340
],

‎docs/OOPS.html

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14278,7 +14278,9 @@ <h2 id="Components:">Components:<a class="anchor-link" href="#Components:">&#182
1427814278
<h2 id="Class:">Class:<a class="anchor-link" href="#Class:">&#182;</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>
1427914279
<h3 id="Definition:">Definition:<a class="anchor-link" href="#Definition:">&#182;</a></h3><p>Using <code>class</code> keyword.
1428014280
Name of a class should start with an uppercase letter.</p>
14281-
<p>Syntax: <code>class Class_name</code></p>
14281+
<p>Syntax:</p>
14282+
<div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Class_name</span>
14283+
</pre></div>
1428214284

1428314285
</div>
1428414286
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs ">
@@ -14301,7 +14303,9 @@ <h3 id="Definition:">Definition:<a class="anchor-link" href="#Definition:">&#182
1430114303
<div class="jp-Cell-inputWrapper"><div class="jp-InputPrompt jp-InputArea-prompt">
1430214304
</div><div class="jp-RenderedHTMLCommon jp-RenderedMarkdown jp-MarkdownOutput " data-mime-type="text/markdown">
1430314305
<h2 id="Object:">Object:<a class="anchor-link" href="#Object:">&#182;</a></h2><p>It is an instance of a class.</p>
14304-
<p>Syntax: <code>obj_name = Class_name()</code></p>
14306+
<p>Syntax:</p>
14307+
<div class="highlight"><pre><span></span><span class="n">obj_name</span> <span class="o">=</span> <span class="n">Class_name</span><span class="p">()</span>
14308+
</pre></div>
1430514309

1430614310
</div>
1430714311
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs ">
@@ -14362,15 +14366,61 @@ <h2 id="Object:">Object:<a class="anchor-link" href="#Object:">&#182;</a></h2><p
1436214366
<div class="jp-Cell-inputWrapper"><div class="jp-InputPrompt jp-InputArea-prompt">
1436314367
</div><div class="jp-RenderedHTMLCommon jp-RenderedMarkdown jp-MarkdownOutput " data-mime-type="text/markdown">
1436414368
<h2 id="self-Keyword:"><em>self</em> Keyword:<a class="anchor-link" href="#self-Keyword:">&#182;</a></h2><p>Calls the attributes for current instance or object.</p>
14365-
<p>Syntax: <code>self.attribute_name</code></p>
14369+
<p>Syntax:</p>
14370+
<div class="highlight"><pre><span></span><span class="bp">self</span><span class="o">.</span><span class="n">attribute_name</span>
14371+
</pre></div>
14372+
14373+
</div>
14374+
</div>
14375+
<div class="jp-Cell-inputWrapper"><div class="jp-InputPrompt jp-InputArea-prompt">
14376+
</div><div class="jp-RenderedHTMLCommon jp-RenderedMarkdown jp-MarkdownOutput " data-mime-type="text/markdown">
14377+
<h2 id="Defining-attributes-in-a-class">Defining attributes in a class<a class="anchor-link" href="#Defining-attributes-in-a-class">&#182;</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">&#182;</a></h3><p>The <code>fields</code> are the variables or containers that store data used and related to an object/Class</p>
14383+
<p>Types:</p>
14384+
<ol>
14385+
<li>Instance fields</li>
14386+
<li>Class or static fields</li>
14387+
</ol>
1436614388

1436714389
</div>
1436814390
</div>
1436914391
<div class="jp-Cell-inputWrapper"><div class="jp-InputPrompt jp-InputArea-prompt">
1437014392
</div><div class="jp-RenderedHTMLCommon jp-RenderedMarkdown jp-MarkdownOutput " data-mime-type="text/markdown">
14371-
<h2 id="Defining-attributes-in-a-class:">Defining attributes in a class:<a class="anchor-link" href="#Defining-attributes-in-a-class:">&#182;</a></h2><p>Attributes or Properties are defined using a constructor.</p>
14372-
<h3 id="Constructor:">Constructor:<a class="anchor-link" href="#Constructor:">&#182;</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>
14403+
<p>Types:</p>
14404+
<ol>
14405+
<li>Instance methods</li>
14406+
<li>Static methods</li>
14407+
</ol>
14408+
14409+
</div>
14410+
</div>
14411+
<div class="jp-Cell-inputWrapper"><div class="jp-InputPrompt jp-InputArea-prompt">
14412+
</div><div class="jp-RenderedHTMLCommon jp-RenderedMarkdown jp-MarkdownOutput " data-mime-type="text/markdown">
14413+
<h3 id="Constructor:">Constructor:<a class="anchor-link" href="#Constructor:">&#182;</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>
14415+
<div class="highlight"><pre><span></span><span class="k">def</span> <span class="fm">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
14416+
<span class="c1"># Custom constructor</span>
14417+
</pre></div>
14418+
<h3 id="Initializer:">Initializer:<a class="anchor-link" href="#Initializer:">&#182;</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>
14420+
<p>Defined as <code>__init__()</code>.</p>
14421+
<div class="highlight"><pre><span></span><span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
14422+
<span class="c1"># Statements for object Initialization</span>
14423+
</pre></div>
1437414424

1437514425
</div>
1437614426
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs ">
@@ -14493,8 +14543,11 @@ <h3 id="Constructor:">Constructor:<a class="anchor-link" href="#Constructor:">&#
1449314543
</div>
1449414544
<div class="jp-Cell-inputWrapper"><div class="jp-InputPrompt jp-InputArea-prompt">
1449514545
</div><div class="jp-RenderedHTMLCommon jp-RenderedMarkdown jp-MarkdownOutput " data-mime-type="text/markdown">
14496-
<h2 id="destructor:">destructor:<a class="anchor-link" href="#destructor:">&#182;</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:">&#182;</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>
14548+
<div class="highlight"><pre><span></span><span class="k">def</span> <span class="fm">__del__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
14549+
<span class="c1"># Custom destructor</span>
14550+
</pre></div>
1449814551

1449914552
</div>
1450014553
</div>
@@ -14559,7 +14612,7 @@ <h2 id="Methods-of-attributes:">Methods of attributes:<a class="anchor-link" hre
1455914612
<div class="jp-Cell-inputWrapper"><div class="jp-InputPrompt jp-InputArea-prompt">
1456014613
</div><div class="jp-RenderedHTMLCommon jp-RenderedMarkdown jp-MarkdownOutput " data-mime-type="text/markdown">
1456114614
<p>Check the complete example <a href="OOPS.py">OOPS.py</a></p>
14562-
<p>Ignore the imports. They have used to provide hinting about the types of each variable</p>
14615+
<p>Ignore the imports. They have been used to provide hinting about the types of each variable</p>
1456314616

1456414617
</div>
1456514618
</div>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /