
{"id":4754,"date":"2020-06-07T20:03:06","date_gmt":"2020-06-08T00:03:06","guid":{"rendered":"https:\/\/ikriv.com\/blog\/?p=4754"},"modified":"2023-09-17T21:21:34","modified_gmt":"2023-09-18T01:21:34","slug":"guido-said-it-would-cause-too-much-disturbance","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=4754","title":{"rendered":"Guido said it would cause too much disturbance&#8230;"},"content":{"rendered":"<p>Python has two ways to convert an object to a string: <code>str(x)<\/code> and <code>repr(x)<\/code>. <code>str<\/code> is supposed to be user-readable, and <code>repr<\/code> is more technical, e.g. for debugging purposes.<\/p>\n<p>To my surprise, if I have a list of things, <code>str(list)<\/code> and <code>repr(list)<\/code> do the same thing and include a <code>repr<\/code> of each item. I would expect <code>str(list)<\/code> to include <code>str<\/code> of the list items, and <code>repr(list)<\/code> to include <code>repr<\/code> of the items.<\/p>\n<p>Apparently, I am not alone: back in 2008 this was proposed by Oleg Broytman and Jim J. Jewett (<a href=\"https:\/\/www.python.org\/dev\/peps\/pep-3140\/\">PEP 3140<\/a>), but rejected, because &#8220;<i>Guido said it would cause too much disturbance too close to Beta<\/i>&#8220;. It is causing disturbance ever since, I suppose&#8230;<\/p>\n<p>Code:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nclass Foo:\r\n    def __init__(self, x):\r\n        self.x = x\r\n    def __str__(self):\r\n        return f&#039;Foo with {self.x}&#039;\r\n    def __repr__(self):\r\n        return f&#039;Foo({repr(self.x)})&#039;\r\n        \r\nfoo = Foo(42)    \r\nprint(repr(foo))           # Foo(42)\r\nprint(str(foo))            # Foo with 42\r\n\r\nlst = &#x5B;Foo(1), Foo(2)]\r\nprint(repr(lst))           # &#x5B;Foo(1), Foo(2)]\r\nprint(str(lst))            # &#x5B;Foo(1), Foo(2)]\r\n# One would expect that str() of list calls str() for each item, but it doesn&#039;t it calls repr() instead\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Python has two ways to convert an object to a string: str(x) and repr(x). str is supposed to be user-readable, and repr is more technical, e.g. for debugging purposes. To <a href=\"https:\/\/ikriv.com\/blog\/?p=4754\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[4],"tags":[],"class_list":["entry","author-ikriv","post-4754","post","type-post","status-publish","format-standard","category-hack"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4754","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4754"}],"version-history":[{"count":8,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4754\/revisions"}],"predecessor-version":[{"id":4888,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4754\/revisions\/4888"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4754"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4754"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4754"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}