{"id":1932,"date":"2023-11-21T16:44:30","date_gmt":"2023-11-21T08:44:30","guid":{"rendered":"http:\/\/zhang.mba\/?p=1932"},"modified":"2023-11-23T09:46:46","modified_gmt":"2023-11-23T01:46:46","slug":"icecream-yi-ge-chao-qiang-de-python-ku","status":"publish","type":"post","link":"https:\/\/zhang.mba\/index.php\/2023\/11\/21\/16\/44\/30\/1932\/icecream-yi-ge-chao-qiang-de-python-ku\/python\/zhangzhiqi\/","title":{"rendered":"icecream\uff0c\u7528\u6700\u5c11\u7684\u4ee3\u7801\u8f7b\u677e\u8fdb\u884c\u4ee3\u7801\u8c03\u8bd5"},"content":{"rendered":"<p><strong>icecream<\/strong><\/p>\n<p><strong><a href=\"https:\/\/github.com\/gruns\/icecream\">https:\/\/github.com\/gruns\/icecream<\/a><\/strong><\/p>\n<p>IceCream \u662f\u4e00\u4e2a Python \u5e93\uff0c<strong>\u53ef\u4ee5\u4f7f\u7528\u6700\u5c11\u7684\u4ee3\u7801\u8f7b\u677e\u8fdb\u884c\u4ee3\u7801\u8c03\u8bd5<\/strong>\u3002<\/p>\n<p>\u8c03\u8bd5\u662f\u8f6f\u4ef6\u5f00\u53d1\u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206\uff0cprint \u8bed\u53e5\u5f88\u65e9\u5c31\u88ab\u7528\u6765\u8c03\u8bd5\u4ee3\u7801\u3002\u7136\u800c\uff0c\u4f20\u7edf\u7684 print \u8bed\u53e5\u7f16\u5199\u8d77\u6765\u53ef\u80fd\u5f88\u4e4f\u5473\uff0c\u5e76\u4e14\u4f1a\u4f7f\u4ee3\u7801\u53d8\u5f97\u6df7\u4e71\u3002<\/p>\n<p>Icecream \u901a\u8fc7\u7528\u7b80\u5355\u7684\u51fd\u6570\u8c03\u7528\u66ff\u6362 print \u8bed\u53e5\uff0c\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u7b80\u6d01\u3001\u66f4\u6613\u8bfb\u7684\u4ee3\u7801\u8c03\u8bd5\u65b9\u6cd5\u3002<\/p>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/mmbiz.qpic.cn\/mmbiz_png\/ibPqnScajrbEkeJMMZrUw2CvmMnrlmNBII6TyyIWFicowT8icmWZczAF4l4TtvjIhHTjwLlIGBichB9RpEjJ8N8aJA\/640?wx_fmt=png&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/ibPqnScajrbEkeJMMZrUw2CvmMnrlmNBII6TyyIWFicowT8icmWZczAF4l4TtvjIhHTjwLlIGBichB9RpEjJ8N8aJA\/640?wx_fmt=png&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\u56fe\u7247\" \/><\/div><\/p>\n<h3><a id=\"%E5%88%9D%E4%BD%93%E9%AA%8C\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u521d\u4f53\u9a8c<\/h3>\n<h4><a id=\"%E5%BA%93%E7%9A%84%E5%AE%89%E8%A3%85\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a><strong>\u5e93\u7684\u5b89\u88c5<\/strong><\/h4>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 pip \u6765\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">pip\u00a0install\u00a0icecream\n<\/code><\/pre>\n<h4><a id=\"%E5%9F%BA%E6%9C%AC%E7%94%A8%E6%B3%95\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/h4>\n<p>icecream \u5e93\u7684\u76f4\u63a5\u7684\u7528\u6cd5\u662f\u7528 \u00a0ic() \u8c03\u7528\u66ff\u6362 print \u8bed\u53e5\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u57fa\u672c\u793a\u4f8b\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">from icecream import ic\ndef multiply(a, b):\n    result = a * b\n    ic(result)\n    return result\nmultiply(2, 3)\n<\/code><\/pre>\n<p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 ic() \u51fd\u6570\u6253\u5370\u4e58\u6cd5\u8fd0\u7b97\u7684\u7ed3\u679c\u3002<\/p>\n<p>\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\u6240\u793a\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">ic|\u00a0result:\u00a06\n<\/code><\/pre>\n<p>\u8bf7\u6ce8\u610f\uff0cic() <strong>\u81ea\u52a8\u5728\u8f93\u51fa\u4e2d\u5305\u542b\u53d8\u91cf\u540d\u79f0<\/strong>\uff0c\u4ece\u800c\u66f4\u5bb9\u6613\u8bc6\u522b\u6b63\u5728\u6253\u5370\u7684\u503c\u3002<\/p>\n<h4><a id=\"%E4%B8%80%E6%AC%A1%E8%BE%93%E5%87%BA%E5%A4%9A%E4%B8%AA%E5%80%BC\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a><strong>\u4e00\u6b21\u8f93\u51fa\u591a\u4e2a\u503c<\/strong><\/h4>\n<p>\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528 ic() \u4e00\u6b21\u6253\u5370\u591a\u4e2a\u503c\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">from icecream import ic\ndef multiply_and_divide(a, b):\n    result = a * b\n    ic(result, a\/b)\n    return result, a\/b\nmultiply_and_divide(2, 3)\n\n# \u8f93\u51fa\u4e3a\n#ic| result: 6, a\/b: 0.6666666666666666\n<\/code><\/pre>\n<p>\u8bf7\u6ce8\u610f\uff0cic() \u5728\u8f93\u51fa\u4e2d\u81ea\u52a8\u7528\u9017\u53f7\u5206\u9694\u503c\u3002<\/p>\n<h4><a id=\"%E5%8F%AF%E8%BF%AD%E4%BB%A3%E5%AF%B9%E8%B1%A1\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a><strong>\u53ef\u8fed\u4ee3\u5bf9\u8c61<\/strong><\/h4>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 ic() \u6253\u5370\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u5185\u5bb9\uff0c\u4f8b\u5982\u5217\u8868\u3001\u5143\u7ec4\u548c\u5b57\u5178\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">from icecream import ic\ndef sum_list(numbers):\n    result = sum(numbers)\n    ic(numbers, result)\n    return result\nsum_list([1, 2, 3, 4, 5])\n# \u8f93\u51fa\u4e3a\n# ic| numbers: [1, 2, 3, 4, 5], result: 15\n<\/code><\/pre>\n<p>\u8bf7\u6ce8\u610f\uff0cic() \u81ea\u52a8\u6253\u5370\u5217\u8868\u7684\u5185\u5bb9\u3002<\/p>\n<h4><a id=\"%E5%87%BD%E6%95%B0\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a><strong>\u51fd\u6570<\/strong><\/h4>\n<p>\u53ef\u4ee5\u4f7f\u7528 ic() \u6253\u5370\u51fd\u6570\u8c03\u7528\u7684\u7ed3\u679c\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">from icecream import ic\ndef multiply(a, b):\n    result = a * b\n    return result\ndef square_and_multiply(a, b):\n    result = multiply(a**2, b)\n    ic(result)\n    return result\nsquare_and_multiply(2, 3)\n# \u8f93\u51fa\u4e3a\n# ic| result: 12\n<\/code><\/pre>\n<p>\u8bf7\u6ce8\u610f\uff0cic() \u81ea\u52a8\u6253\u5370\u51fd\u6570\u7684\u8fd4\u56de\u503c\u3002<\/p>\n<h4><a id=\"%E8%A1%A8%E8%BE%BE%E5%BC%8F\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a><strong>\u8868\u8fbe\u5f0f<\/strong><\/h4>\n<p>\u53ef\u4ee5\u4f7f\u7528 ic() \u6253\u5370\u8868\u8fbe\u5f0f\u7684\u503c\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">from icecream import ic\ndef multiply(a, b):\n    result = a * b\n    ic(result)\n    return result\ndef square_and_multiply(a, b):\n    ic(a**2 * b)\n    result = multiply(a**2, b)\n    return result\nsquare_and_multiply(2, 3)\n\n# \u8f93\u51fa\u4e3a\n# ic| a**2 * b: 12\n# ic| result: 12\n<\/code><\/pre>\n<h4><a id=\"%E6%A8%A1%E5%9D%97%E5%92%8C%E7%B1%BB\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a><strong>\u6a21\u5757\u548c\u7c7b<\/strong><\/h4>\n<p>\u53ef\u4ee5\u4f7f\u7528 ic() \u6253\u5370\u6a21\u5757\u548c\u7c7b\u7684\u5185\u5bb9\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">from icecream import ic\nimport math\nclass Circle:\n    def __init__(self, radius):\n        self.radius = radius\n    \n    def area(self):\n        result = math.pi * self.radius ** 2\n        ic(result)\n        return result\nc = Circle(2)\nc.area()\n\n# \u8f93\u51fa\u4e3a\n# ic| result: 12.566370614359172\n<\/code><\/pre>\n<p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 ic() \u6765\u6253\u5370 Circle \u7c7b\u4e2d\u5706\u7684\u9762\u79ef\u3002<\/p>\n<h4><a id=\"%E8%87%AA%E5%AE%9A%E4%B9%89%E6%A0%BC%E5%BC%8F\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a><strong>\u81ea\u5b9a\u4e49\u683c\u5f0f<\/strong><\/h4>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 format() \u65b9\u6cd5\u81ea\u5b9a\u4e49 ic() \u7684\u8f93\u51fa\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">from icecream import ic\ndef multiply(a, b):\n    result = a * b\n    ic('The result of multiplying {a} and {b} is {result}'.format(a=a, b=b, result=result))\n    return result\nmultiply(2, 3)\n\n# \u8f93\u51fa\u4e3a\n# ic| 'The result of multiplying {a} and {b} is {result}'.format(a=a, b=b, result=result): 'The result of multiplying 2 and 3 is 6'\n<\/code><\/pre>\n<p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 ic() \u6253\u5370\u4e00\u6761\u5305\u542b\u8f93\u5165\u503c\u548c\u7ed3\u679c\u7684\u81ea\u5b9a\u4e49\u6d88\u606f\u3002<\/p>\n<h4><a id=\"%E8%87%AA%E5%AE%9A%E4%B9%89%E5%89%8D%E7%BC%80\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a><strong>\u81ea\u5b9a\u4e49\u524d\u7f00<\/strong><\/h4>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 ic.configureOutput() \u6765\u81ea\u5b9a\u4e49\u8f93\u51fa\u524d\u7f00\u3002prefix \u53ef\u4ee5\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u4e5f\u53ef\u4ee5\u662f\u4e00\u4e2a\u51fd\u6570\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">from icecream import ic\nimport time\n\ndef unixTimestamp():\n  return '%i |&gt; ' % int(time.time())\n\nic.configureOutput(prefix=unixTimestamp)\n\ndef multiply(a, b):\n    result = a * b\n    ic(result)\n    return result\nmultiply(2, 3)\n\n# \u8f93\u51fa\u4e3a\n# 1696520290 |&gt; result: 6\n<\/code><\/pre>\n<p>IceCream \u662f\u4e00\u4e2a\u5f3a\u5927\u7684 Python \u4ee3\u7801\u8c03\u8bd5\u5de5\u5177\uff0c\u53ef\u4ee5\u66ff\u4ee3\u4f20\u7edf\u7684 print \u8bed\u53e5\u3002<\/p>\n<p>\u5b83\u63d0\u4f9b\u6709\u5173\u53d8\u91cf\u3001\u8868\u8fbe\u5f0f\u548c\u51fd\u6570\u8c03\u7528\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u4f7f\u4f60\u53ef\u4ee5\u8f7b\u677e\u8bc6\u522b\u548c\u4fee\u590d\u4ee3\u7801\u4e2d\u7684\u9519\u8bef\u3002<\/p>\n<p>\u6765\u6e90&#8212;&#8211;\u7a0b\u5e8f\u5458\u5c0f\u5bd2<\/p>\n<!--CusAds0-->\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>icecream https:\/\/github.com\/gruns\/icecream IceCream \u662f\u4e00\u4e2a Python \u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u6700\u5c11\u7684\u4ee3\u7801\u8f7b\u677e\u8fdb\u884c\u4ee3\u7801\u8c03\u8bd5\u3002 \u8c03\u8bd5\u662f\u8f6f\u4ef6\u5f00\u53d1\u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206\uff0cprint \u8bed\u53e5\u5f88\u65e9\u5c31\u88ab\u7528\u6765\u8c03\u8bd5\u4ee3\u7801\u3002\u7136\u800c\uff0c\u4f20\u7edf\u7684 print \u8bed\u53e5\u7f16\u5199\u8d77\u6765\u53ef\u80fd\u5f88\u4e4f\u5473\uff0c\u5e76\u4e14\u4f1a\u4f7f\u4ee3\u7801\u53d8\u5f97\u6df7\u4e71\u3002 I &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0},"categories":[12],"tags":[54],"_links":{"self":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts\/1932"}],"collection":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/comments?post=1932"}],"version-history":[{"count":0,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts\/1932\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/media?parent=1932"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/categories?post=1932"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/tags?post=1932"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}