{"id":13689,"date":"2021-06-16T13:08:43","date_gmt":"2021-06-16T07:38:43","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=13689"},"modified":"2021-06-16T13:08:49","modified_gmt":"2021-06-16T07:38:49","slug":"repr","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/repr\/","title":{"rendered":"Repr(): Discovering a Special Method in Python"},"content":{"rendered":"\n<p>Python consists of several special methods. The names of these special methods are preceded and followed by two underscore characters &#8211;  <em>__methodname__()<\/em>. With these special methods, we can initialize, compare or print objects. These methods can be called when the objects are created. In this article, we shall be talking about one such special method in by &#8211; the repr method, which is represented by <strong>__repr__()<\/strong>.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_74 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #990303;color:#990303\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #990303;color:#990303\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.pythonpool.com\/repr\/#What_is_the_repr_method\" >What is the repr method?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.pythonpool.com\/repr\/#The_syntax_of_the_repr_method_is\" >The syntax of the repr() method is:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.pythonpool.com\/repr\/#Working_of_the_repr_function\" >Working of the repr function<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pythonpool.com\/repr\/#Using_repr_to_call_class_objects\" >Using repr to call class objects<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/repr\/#The_Entire_Code_is\" >The Entire Code is:<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/repr\/#_repr_vs_str\" >__repr__() vs __str__()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.pythonpool.com\/repr\/#FAQs\" >FAQ&#8217;s<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-what-is-the-repr-method\"><span class=\"ez-toc-section\" id=\"What_is_the_repr_method\"><\/span>What is the repr method?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><strong>The repr method in python is used to generate a string representation of a given object. When the repr() method is invoked, it will automatically call the object&#8217;s __repr__() function. It will return a string which would be a printable representation of the given object. <\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-the-syntax-of-the-repr-method-is\"><span class=\"ez-toc-section\" id=\"The_syntax_of_the_repr_method_is\"><\/span><strong>The syntax of the repr() method is:<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong><em>repr(obj)<\/em><\/strong><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong>It accepts only one argument which is the object whose string representation has to be printed. <\/strong><\/p><\/blockquote>\n\n\n\n<p><strong>We can also override the __repr__() method to change the working of the repr() function.  <\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-working-of-the-repr-function\"><span class=\"ez-toc-section\" id=\"Working_of_the_repr_function\"><\/span>Working of the repr function<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><strong>Let us now understand how the repr function works in python. <\/strong><\/p>\n\n\n\n<p>Here, we will try to print a variable <em>&#8216;city&#8217; <\/em>containing the string value<em> &#8216;Chicago&#8217; <\/em>enclosed in single quotes. First, we shall print the<em> &#8216;city&#8217; <\/em>variable. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ncity = 'Chicago'\nprint(city)\n<\/pre><\/div>\n\n\n<p><strong>The output will be the variable data without any quotes.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Chicago<\/pre>\n\n\n\n<p>But, if we use the repr function, it will return &#8216;Chicago&#8217; after wrapping it in double quotes. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ncity = 'Chicago'\nrepr(city)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">''Chicago''<\/pre>\n\n\n\n<p>If we use<em> print() along with<\/em> the<em> repr() <\/em>function, then it will print the expression as it was passed. Here, the string <em>&#8216;<em><a href=\"https:\/\/en.wikipedia.org\/wiki\/Chicago\" target=\"_blank\" rel=\"noreferrer noopener\">Chicago<\/a><\/em>&#8216;<\/em> will be printed in single quotes.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(repr(city))\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">'Chicago'<\/pre>\n\n\n\n<p>For numeric values also, the <em>repr() <\/em>function will return a printable representation for that variable.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nx = 11\nrepr(x)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">'11'<\/pre>\n\n\n\n<p><strong>This will hold true for boolean values also. <\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nx = True\nrepr(x)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">'True'<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-using-repr-to-call-class-objects\"><span class=\"ez-toc-section\" id=\"Using_repr_to_call_class_objects\"><\/span>Using repr to call class objects<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The repr() function can also be called by class objects. <\/p>\n\n\n\n<p><strong>Let us take a user-defined class named <em>&#8216;Product&#8217;<\/em>. There will be two variables in the product class &#8211; <em>&#8216;name&#8217; <\/em>and<em> &#8216;price&#8217;<\/em>. We will initialize the variables in the <em>__init__()<\/em> function first.<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass Product:\n  def __init__(self, name, price):\n    self.name = name\n    self.price = price\n<\/pre><\/div>\n\n\n<p>Now, after creating the class Product, we shall create an object of the Product class. We shall pass two values to the object.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">obj = Product('Bread', 50)<\/pre>\n\n\n\n<p>After creating the object, we shall <a href=\"http:\/\/www.pythonpool.com\/python-pass\/\" target=\"_blank\" rel=\"noreferrer noopener\">pass<\/a> the object as the argument to the<em> repr()<\/em> function. The <em>repr()<\/em> function will convert object to strings as and when required. We shall now print the <em>repr() <\/em>function&#8217;s output. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(obj)\n<\/pre><\/div>\n\n\n<p>The output is:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;__main__.Product object at 0x7fb6351fc750&gt;<\/pre>\n\n\n\n<p>Here we received the object of Product class inside angular brackets. It contains the name and location of the object. To print the actual object values, we shall override the <strong><em>__repr__() <\/em>function.<\/strong><\/p>\n\n\n\n<p>So, we shall override the  <em>__repr__()<\/em> function to print the &#8216;name&#8217; and &#8216;price&#8217; of the object of Product class. It shall return a formatted string containing the name and age.  <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass Product:\n  def __init__(self, name, price):\n    self.name = name\n    self.price = price\n  \n  def __repr__(self):\n    return f'Product Details : name = {self.name}, price = {self.price}'\n<\/pre><\/div>\n\n\n<p><strong>Now, after creating the class Product, we shall create an object of Product class. We shall pass two values to the object.<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nobj = Product('Bread', 50)\n<\/pre><\/div>\n\n\n<p>We shall now print the<em> repr() <\/em>function&#8217;s output. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(obj)\n<\/pre><\/div>\n\n\n<p><strong>The output is:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Product Details : name = Bread, price = 50<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-the-entire-code-is\"><span class=\"ez-toc-section\" id=\"The_Entire_Code_is\"><\/span>The Entire Code is:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#Without overriding\n\nclass Product:\n  def __init__(self, name, price):\n    self.name = name\n    self.price = price\n\nobj = Product('Bread', 50)\n\nprint(obj)\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#With overriding\n\nclass Product:\n  def __init__(self, name, price):\n    self.name = name\n    self.price = price\n  \n  def __repr__(self):\n    return f'Product Details : name = {self.name}, price = {self.price}'\n\nobj = Product('Bread', 50)\n\nprint(obj)\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-__repr__-vs-__str__\"><span class=\"ez-toc-section\" id=\"_repr_vs_str\"><\/span>__repr__() vs __str__()<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>We use both the special functions &#8211; <em>__repr__()<\/em> and <em>__str__()<\/em> for obtaining string representation of a given object. The difference between the two is that the <em>__str__() <\/em>function will return a value which is user friendly whereas the <em>__repr__()<\/em> function will return a value in its unambiguous form. <\/p>\n\n\n\n<p>Suppose if we take a string &#8216;python&#8217; and pass it to both the functions. Then, <em>__str__() <\/em>will print the string as it is passed by the user whereas the<em> __repr__() <\/em>function will pass the string as it is represented in the string format.<\/p>\n\n\n\n<p>For <em>__str__()<\/em>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nx = 'python'\nstr(x)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">'python'<\/pre>\n\n\n\n<p><strong>For <em>__repr__()<\/em>:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nrepr(x)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n''python''\n<\/pre><\/div>\n\n\n<p>For printing date, the <em>__str()__<\/em> function will only print the current date. For that, we will import datetime and print it for the current time.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport datetime\ndate = datetime.datetime.now()\nstr(date)\n<\/pre><\/div>\n\n\n<p><strong>The output will be:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">'2021-06-15 11:15:53.494880'<\/pre>\n\n\n\n<p><strong>But, if we use the <em>repr()<\/em> function, then it will print the official form of representation of the &#8216;date&#8217; object.<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport datetime\ndate = datetime.datetime.now()\nrepr(date)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">'datetime.datetime(2021, 6, 15, 11, 15, 53, 494880)'<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-faq-s\"><span class=\"ez-toc-section\" id=\"FAQs\"><\/span>FAQ&#8217;s<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1623757535945\"><strong class=\"schema-faq-question\">Which is better &#8211; __repr__() or __str__()?<br\/><\/strong> <p class=\"schema-faq-answer\">Which function is better, actually depends upon you and your requirements. If your aim is to use it for the users, then <em>__str__()<\/em> is ideal. But if you want to use it for developers to aid in debugging, then <em>__repr__() <\/em>is preferred. <\/p> <\/div> <\/div>\n\n\n\n<p>That was all for repr in python. If you have any questions, let us know in the comments below.<\/p>\n\n\n\n<p><em>Until next time, Keep Learning!<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python consists of several special methods. The names of these special methods are preceded and followed by two underscore characters &#8211; __methodname__(). With these special &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Repr(): Discovering a Special Method in Python\" class=\"read-more button\" href=\"https:\/\/www.pythonpool.com\/repr\/#more-13689\" aria-label=\"More on Repr(): Discovering a Special Method in Python\">Read more<\/a><\/p>\n","protected":false},"author":20,"featured_media":13728,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[15],"tags":[4218,4219,4221,4222,4224,4223],"class_list":["post-13689","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-__repr__","tag-python-__repr__","tag-python-repr","tag-python-str-vs-repr","tag-repr-python","tag-repr-python-3","infinite-scroll-item"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.1 (Yoast SEO v25.0) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Repr(): Discovering a Special Method in Python - Python Pool<\/title>\n<meta name=\"description\" content=\"The repr method in python is used to generate a string representation of a given object. Inside a class, it will invoke __repr__() function.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pythonpool.com\/repr\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Repr(): Discovering a Special Method in Python\" \/>\n<meta property=\"og:description\" content=\"Python consists of several special methods. The names of these special methods are preceded and followed by two underscore characters - __methodname__().\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/repr\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2021-06-16T07:38:43+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-06-16T07:38:49+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/06\/Repr-Discovering-a-Special-Method-in-Python.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1350\" \/>\n\t<meta property=\"og:image:height\" content=\"650\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Dhruvi Vikma\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:site\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Dhruvi Vikma\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.pythonpool.com\/repr\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/repr\/\"},\"author\":{\"name\":\"Dhruvi Vikma\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/bed8fc40c7b71baf7d76b1cfefd79f23\"},\"headline\":\"Repr(): Discovering a Special Method in Python\",\"datePublished\":\"2021-06-16T07:38:43+00:00\",\"dateModified\":\"2021-06-16T07:38:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/repr\/\"},\"wordCount\":719,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/repr\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/06\/Repr-Discovering-a-Special-Method-in-Python.jpg\",\"keywords\":[\"__repr__\",\"python __repr__\",\"python repr()\",\"python str vs repr\",\"repr python\",\"repr python 3\"],\"articleSection\":[\"Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.pythonpool.com\/repr\/#respond\"]}]},{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\/\/www.pythonpool.com\/repr\/\",\"url\":\"https:\/\/www.pythonpool.com\/repr\/\",\"name\":\"Repr(): Discovering a Special Method in Python - Python Pool\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/repr\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/repr\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/06\/Repr-Discovering-a-Special-Method-in-Python.jpg\",\"datePublished\":\"2021-06-16T07:38:43+00:00\",\"dateModified\":\"2021-06-16T07:38:49+00:00\",\"description\":\"The repr method in python is used to generate a string representation of a given object. Inside a class, it will invoke __repr__() function.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.pythonpool.com\/repr\/#breadcrumb\"},\"mainEntity\":[{\"@id\":\"https:\/\/www.pythonpool.com\/repr\/#faq-question-1623757535945\"}],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.pythonpool.com\/repr\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/repr\/#primaryimage\",\"url\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/06\/Repr-Discovering-a-Special-Method-in-Python.jpg\",\"contentUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/06\/Repr-Discovering-a-Special-Method-in-Python.jpg\",\"width\":1350,\"height\":650,\"caption\":\"repr\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.pythonpool.com\/repr\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.pythonpool.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Repr(): Discovering a Special Method in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.pythonpool.com\/#website\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"name\":\"Python Pool\",\"description\":\"Your One-Stop Python Learning Destination\",\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.pythonpool.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.pythonpool.com\/#organization\",\"name\":\"Python Pool\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"contentUrl\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"width\":452,\"height\":185,\"caption\":\"Python Pool\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/pythonpool\",\"https:\/\/www.youtube.com\/c\/pythonpool\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/bed8fc40c7b71baf7d76b1cfefd79f23\",\"name\":\"Dhruvi Vikma\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/520d8a37e8b6ce4e6df57e997dc0d571225f48098bbb02838c3884bb603e886f?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/520d8a37e8b6ce4e6df57e997dc0d571225f48098bbb02838c3884bb603e886f?s=96&d=wavatar&r=g\",\"caption\":\"Dhruvi Vikma\"}},{\"@type\":\"Question\",\"@id\":\"https:\/\/www.pythonpool.com\/repr\/#faq-question-1623757535945\",\"position\":1,\"url\":\"https:\/\/www.pythonpool.com\/repr\/#faq-question-1623757535945\",\"name\":\"Which is better - __repr__() or __str__()?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Which function is better, actually depends upon you and your requirements. If your aim is to use it for the users, then <em>__str__()<\/em> is ideal. But if you want to use it for developers to aid in debugging, then <em>__repr__() <\/em>is preferred. \",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Repr(): Discovering a Special Method in Python - Python Pool","description":"The repr method in python is used to generate a string representation of a given object. Inside a class, it will invoke __repr__() function.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pythonpool.com\/repr\/","og_locale":"en_US","og_type":"article","og_title":"Repr(): Discovering a Special Method in Python","og_description":"Python consists of several special methods. The names of these special methods are preceded and followed by two underscore characters - __methodname__().","og_url":"https:\/\/www.pythonpool.com\/repr\/","og_site_name":"Python Pool","article_published_time":"2021-06-16T07:38:43+00:00","article_modified_time":"2021-06-16T07:38:49+00:00","og_image":[{"width":1350,"height":650,"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/06\/Repr-Discovering-a-Special-Method-in-Python.jpg","type":"image\/jpeg"}],"author":"Dhruvi Vikma","twitter_card":"summary_large_image","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Dhruvi Vikma","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonpool.com\/repr\/#article","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/repr\/"},"author":{"name":"Dhruvi Vikma","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/bed8fc40c7b71baf7d76b1cfefd79f23"},"headline":"Repr(): Discovering a Special Method in Python","datePublished":"2021-06-16T07:38:43+00:00","dateModified":"2021-06-16T07:38:49+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/repr\/"},"wordCount":719,"commentCount":0,"publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/www.pythonpool.com\/repr\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/06\/Repr-Discovering-a-Special-Method-in-Python.jpg","keywords":["__repr__","python __repr__","python repr()","python str vs repr","repr python","repr python 3"],"articleSection":["Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonpool.com\/repr\/#respond"]}]},{"@type":["WebPage","FAQPage"],"@id":"https:\/\/www.pythonpool.com\/repr\/","url":"https:\/\/www.pythonpool.com\/repr\/","name":"Repr(): Discovering a Special Method in Python - Python Pool","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pythonpool.com\/repr\/#primaryimage"},"image":{"@id":"https:\/\/www.pythonpool.com\/repr\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/06\/Repr-Discovering-a-Special-Method-in-Python.jpg","datePublished":"2021-06-16T07:38:43+00:00","dateModified":"2021-06-16T07:38:49+00:00","description":"The repr method in python is used to generate a string representation of a given object. Inside a class, it will invoke __repr__() function.","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/repr\/#breadcrumb"},"mainEntity":[{"@id":"https:\/\/www.pythonpool.com\/repr\/#faq-question-1623757535945"}],"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonpool.com\/repr\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/repr\/#primaryimage","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/06\/Repr-Discovering-a-Special-Method-in-Python.jpg","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/06\/Repr-Discovering-a-Special-Method-in-Python.jpg","width":1350,"height":650,"caption":"repr"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonpool.com\/repr\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"Repr(): Discovering a Special Method in Python"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonpool.com\/#website","url":"https:\/\/www.pythonpool.com\/","name":"Python Pool","description":"Your One-Stop Python Learning Destination","publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pythonpool.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.pythonpool.com\/#organization","name":"Python Pool","url":"https:\/\/www.pythonpool.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/","url":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","contentUrl":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","width":452,"height":185,"caption":"Python Pool"},"image":{"@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/pythonpool","https:\/\/www.youtube.com\/c\/pythonpool"]},{"@type":"Person","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/bed8fc40c7b71baf7d76b1cfefd79f23","name":"Dhruvi Vikma","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/520d8a37e8b6ce4e6df57e997dc0d571225f48098bbb02838c3884bb603e886f?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/520d8a37e8b6ce4e6df57e997dc0d571225f48098bbb02838c3884bb603e886f?s=96&d=wavatar&r=g","caption":"Dhruvi Vikma"}},{"@type":"Question","@id":"https:\/\/www.pythonpool.com\/repr\/#faq-question-1623757535945","position":1,"url":"https:\/\/www.pythonpool.com\/repr\/#faq-question-1623757535945","name":"Which is better - __repr__() or __str__()?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"Which function is better, actually depends upon you and your requirements. If your aim is to use it for the users, then <em>__str__()<\/em> is ideal. But if you want to use it for developers to aid in debugging, then <em>__repr__() <\/em>is preferred. ","inLanguage":"en-US"},"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/13689","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/users\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=13689"}],"version-history":[{"count":20,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/13689\/revisions"}],"predecessor-version":[{"id":13729,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/13689\/revisions\/13729"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/13728"}],"wp:attachment":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=13689"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=13689"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=13689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}