gruns / icecream

🍦 Never use print() to debug again.
MIT License
9.22k stars 187 forks source link

ic() and print() outputs different #111

Closed blue-sky-ling closed 2 years ago

blue-sky-ling commented 2 years ago
from icecream import ic
from pyquery import PyQuery as pq

>>>html = """
<div id="container">
    <ul class="list">
         <li class="item-0">first item</li>
         <li class="item-1"><a href="link2.html">second item</a></li>
         <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li>
         <li class="item-1 active"><a href="link4.html">fourth item</a></li>
         <li class="item-0"><a href="link5.html">fifth item</a></li>
     </ul>
 </div>
"""
>>>doc=pq(html)

>>>ic(doc("li"))
ic| doc("li"): [<li.item-0>, <li.item-1>, <li.item-0.active>, <li.item-1.active>, <li.item-0>]

>>>print(doc("li"))
<li class="item-0">first item</li>
         <li class="item-1"><a href="link2.html">second item</a></li>
         <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li>
         <li class="item-1 active"><a href="link4.html">fourth item</a></li>
         <li class="item-0"><a href="link5.html">fifth item</a></li>

Question: Why are 'ic (doc ("li")) 'and 'print (doc ("li")) 'outputs different?

alexmojaki commented 2 years ago

print uses str whereas ic uses pprint by default which uses repr.

>>> doc("li")
[<li.item-0>, <li.item-1>, <li.item-0.active>, <li.item-1.active>, <li.item-0>]
>>> pprint.pprint(doc("li"))
[<li.item-0>, <li.item-1>, <li.item-0.active>, <li.item-1.active>, <li.item-0>]
>>> ic.configureOutput(argToStringFunction=str)
>>> ic(doc("li"))
ic| doc("li"): <li class="item-0">first item</li>
                        <li class="item-1"><a href="link2.html">second item</a></li>
                        <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li>
                        <li class="item-1 active"><a href="link4.html">fourth item</a></li>
                        <li class="item-0"><a href="link5.html">fifth item</a></li>