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: docs/prodigiouspython/Chapter_1/10_String_representations_of_objects.html
+28-28Lines changed: 28 additions & 28 deletions
Original file line number
Diff line number
Diff line change
@@ -546,38 +546,38 @@ <h1><span class="section-number">10. </span>String representations of objects: s
546
546
<p>Python’s official documentations states that <codeclass="docutils literal notranslate"><spanclass="pre">__str__</span></code> should be used to represent a object which is human readable(informal), whereas <codeclass="docutils literal notranslate"><spanclass="pre">__repr__</span></code> is used for official representation of an object.</p>
print(f"The repr of now is: {repr(now)}")
554
-
print(f"The str of now is: {str(now)}")
553
+
<spanclass="nb">print</span><spanclass="p">(</span><spanclass="sa">f</span><spanclass="s2">"The repr of now is: </span><spanclass="si">{</span><spanclass="nb">repr</span><spanclass="p">(</span><spanclass="n">now</span><spanclass="p">)</span><spanclass="si">}</span><spanclass="s2">"</span><spanclass="p">)</span>
554
+
<spanclass="nb">print</span><spanclass="p">(</span><spanclass="sa">f</span><spanclass="s2">"The str of now is: </span><spanclass="si">{</span><spanclass="nb">str</span><spanclass="p">(</span><spanclass="n">now</span><spanclass="p">)</span><spanclass="si">}</span><spanclass="s2">"</span><spanclass="p">)</span>
555
555
</pre></div>
556
556
</div>
557
557
</div>
558
558
<divclass="cell_output docutils container">
559
-
<divclass="output stream highlight-myst-ansi notranslate"><divclass="highlight"><pre><span></span>The repr of now is: datetime.datetime(2023, 1, 2, 6, 55, 59, 494226)
560
-
The str of now is: 2023-01-02 06:55:59.494226
559
+
<divclass="output stream highlight-myst-ansi notranslate"><divclass="highlight"><pre><span></span>The repr of now is: datetime.datetime(2023, 2, 5, 6, 59, 56, 776419)
<spanclass="nb">print</span><spanclass="p">(</span><spanclass="sa">f</span><spanclass="s2">"The repr of language_obj is: </span><spanclass="si">{</span><spanclass="nb">repr</span><spanclass="p">(</span><spanclass="n">language_obj</span><spanclass="p">)</span><spanclass="si">}</span><spanclass="s2">"</span><spanclass="p">)</span>
574
+
<spanclass="nb">print</span><spanclass="p">(</span><spanclass="sa">f</span><spanclass="s2">"The str of language_obj is: </span><spanclass="si">{</span><spanclass="nb">str</span><spanclass="p">(</span><spanclass="n">language_obj</span><spanclass="p">)</span><spanclass="si">}</span><spanclass="s2">"</span><spanclass="p">)</span>
575
575
</pre></div>
576
576
</div>
577
577
</div>
578
578
<divclass="cell_output docutils container">
579
-
<divclass="output stream highlight-myst-ansi notranslate"><divclass="highlight"><pre><span></span>The repr of language_obj is: <__main__.ProgrammingLanguage object at 0x7f232451dcc0>
580
-
The str of language_obj is: <__main__.ProgrammingLanguage object at 0x7f232451dcc0>
579
+
<divclass="output stream highlight-myst-ansi notranslate"><divclass="highlight"><pre><span></span>The repr of language_obj is: <__main__.ProgrammingLanguage object at 0x7ff940197100>
580
+
The str of language_obj is: <__main__.ProgrammingLanguage object at 0x7ff940197100>
581
581
</pre></div>
582
582
</div>
583
583
</div>
@@ -586,28 +586,28 @@ <h1><span class="section-number">10. </span>String representations of objects: s
586
586
<p>Now let’s try to override the <codeclass="docutils literal notranslate"><spanclass="pre">__str__</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">__repr__</span></code> methods and see how the representations work</p>
<spanclass="k">return</span><spanclass="sa">f</span><spanclass="s2">"I am </span><spanclass="si">{</span><spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">name</span><spanclass="si">}</span><spanclass="s2">of age </span><spanclass="si">{</span><spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">age</span><spanclass="si">}</span><spanclass="s2">"</span>
597
597
598
-
# overriding __repr__ method
599
-
def__repr__(self):
600
-
return f"Human(name={self.name}, age={self.age}) object at {hex(id(self))}"
<spanclass="k">return</span><spanclass="sa">f</span><spanclass="s2">"Human(name=</span><spanclass="si">{</span><spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">name</span><spanclass="si">}</span><spanclass="s2">, age=</span><spanclass="si">{</span><spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">age</span><spanclass="si">}</span><spanclass="s2">) object at </span><spanclass="si">{</span><spanclass="nb">hex</span><spanclass="p">(</span><spanclass="nb">id</span><spanclass="p">(</span><spanclass="bp">self</span><spanclass="p">))</span><spanclass="si">}</span><spanclass="s2">"</span>
<spanclass="nb">print</span><spanclass="p">(</span><spanclass="sa">f</span><spanclass="s2">"The repr of human_obj is: </span><spanclass="si">{</span><spanclass="nb">repr</span><spanclass="p">(</span><spanclass="n">human_obj</span><spanclass="p">)</span><spanclass="si">}</span><spanclass="s2">"</span><spanclass="p">)</span>
605
+
<spanclass="nb">print</span><spanclass="p">(</span><spanclass="sa">f</span><spanclass="s2">"The str of human_obj is: </span><spanclass="si">{</span><spanclass="nb">str</span><spanclass="p">(</span><spanclass="n">human_obj</span><spanclass="p">)</span><spanclass="si">}</span><spanclass="s2">"</span><spanclass="p">)</span>
606
606
</pre></div>
607
607
</div>
608
608
</div>
609
609
<divclass="cell_output docutils container">
610
-
<divclass="output stream highlight-myst-ansi notranslate"><divclass="highlight"><pre><span></span>The repr of human_obj is: Human(name=IronMan, age=48) object at 0x7f232451faf0
610
+
<divclass="output stream highlight-myst-ansi notranslate"><divclass="highlight"><pre><span></span>The repr of human_obj is: Human(name=IronMan, age=48) object at 0x7ff9401955a0
<divclass="highlight-ipython3 notranslate"><divclass="highlight"><pre><span></span># Help utility on getcwd function of sys module
576
-
help(os.getcwd)
575
+
<divclass="highlight-ipython3 notranslate"><divclass="highlight"><pre><span></span><spanclass="c1"># Help utility on getcwd function of sys module</span>
0 commit comments