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
@@ -547,38 +546,38 @@ <h1><span class="section-number">10. </span>String representations of objects: s
547
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>
<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>
555
-
<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>
553
+
print(f"The repr of now is: {repr(now)}")
554
+
print(f"The str of now is: {str(now)}")
556
555
</pre></div>
557
556
</div>
558
557
</div>
559
558
<divclass="cell_output docutils container">
560
-
<divclass="output stream highlight-myst-ansi notranslate"><divclass="highlight"><pre><span></span>The repr of now is: datetime.datetime(2022, 11, 25, 12, 33, 32, 194519)
561
-
The str of now is: 2022-11-25 12:33:32.194519
559
+
<divclass="output stream highlight-myst-ansi notranslate"><divclass="highlight"><pre><span></span>The repr of now is: datetime.datetime(2022, 12, 22, 16, 57, 5, 41228)
<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>
575
-
<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>
print(f"The repr of language_obj is: {repr(language_obj)}")
574
+
print(f"The str of language_obj is: {str(language_obj)}")
576
575
</pre></div>
577
576
</div>
578
577
</div>
579
578
<divclass="cell_output docutils container">
580
-
<divclass="output stream highlight-myst-ansi notranslate"><divclass="highlight"><pre><span></span>The repr of language_obj is: <__main__.ProgrammingLanguage object at 0x7f076f59c3d0>
581
-
The str of language_obj is: <__main__.ProgrammingLanguage object at 0x7f076f59c3d0>
579
+
<divclass="output stream highlight-myst-ansi notranslate"><divclass="highlight"><pre><span></span>The repr of language_obj is: <__main__.ProgrammingLanguage object at 0x7fe6834d8220>
580
+
The str of language_obj is: <__main__.ProgrammingLanguage object at 0x7fe6834d8220>
582
581
</pre></div>
583
582
</div>
584
583
</div>
@@ -587,28 +586,28 @@ <h1><span class="section-number">10. </span>String representations of objects: s
587
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>
594
+
# overriding __str__ method
595
+
def__str__(self):
596
+
return f"I am {self.name} of age {self.age}"
<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>
598
+
# overriding __repr__ method
599
+
def__repr__(self):
600
+
return f"Human(name={self.name}, age={self.age}) object at {hex(id(self))}"
<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>
606
-
<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>
print(f"The repr of human_obj is: {repr(human_obj)}")
605
+
print(f"The str of human_obj is: {str(human_obj)}")
607
606
</pre></div>
608
607
</div>
609
608
</div>
610
609
<divclass="cell_output docutils container">
611
-
<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 0x7f076f59c340
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 0x7fe6834d9f00
<divclass="highlight-ipython3 notranslate"><divclass="highlight"><pre><span></span><spanclass="c1"># Help utility on getcwd function of sys module</span>
0 commit comments