{"id":154,"date":"2024-01-25T13:51:26","date_gmt":"2024-01-25T13:51:26","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=154"},"modified":"2024-01-25T13:51:28","modified_gmt":"2024-01-25T13:51:28","slug":"python-__repr__","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/25\/python-__repr__\/","title":{"rendered":"Python __repr__"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn how to use the Python&nbsp;<code>__repr__<\/code>&nbsp;dunder method and the difference between the&nbsp;<code>__repr__<\/code>&nbsp;and&nbsp;<code>__str__<\/code>&nbsp;methods.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the Python&nbsp;<code>__repr__<\/code>&nbsp;magic method<\/h2>\n\n\n\n<p>The\u00a0<code>__repr__<\/code>\u00a0dunder method defines behavior when you pass an instance of a\u00a0class\u00a0to the\u00a0<code>repr()<\/code>.<\/p>\n\n\n\n<p>The&nbsp;<code>__repr__<\/code>&nbsp;method returns the string representation of an object. Typically, the&nbsp;<code>__repr__()<\/code>&nbsp;returns a string that can be executed and yield the same value as the object.<\/p>\n\n\n\n<p>In other words, if you pass the returned string of the&nbsp;<code>object_name.__repr__()<\/code>&nbsp;method to the&nbsp;<code>eval()<\/code>&nbsp;function, you\u2019ll get the same value as the&nbsp;<code>object_name<\/code>. Let\u2019s take a look at an example.<\/p>\n\n\n\n<p>First, define the\u00a0<code>Person<\/code>\u00a0class with three instance attributes\u00a0<code>first_name<\/code>,\u00a0<code>last_name<\/code>, and\u00a0<code>age<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Second, create a new instance of the\u00a0<code>Person<\/code>\u00a0class and display its string representation:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>person = Person('John', 'Doe', 25) print(repr(person))<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&lt;__main__.Person object at 0x000001F51B3313A0><\/code><small>Code language: HTML, XML (xml)<\/small><\/code><\/pre>\n\n\n\n<p>By default, the output contains the memory address of the\u00a0<code>person<\/code>\u00a0object. To customize the string representation of the object, you can implement the\u00a0<code>__repr__<\/code>\u00a0method like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age def __repr__(self): return f'Person(\"{self.first_name}\",\"{self.last_name}\",{self.age})'<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>When you pass an instance of the\u00a0<code>Person<\/code>\u00a0class to the\u00a0<code>repr()<\/code>, Python will call the\u00a0<code>__repr__<\/code>\u00a0method automatically. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>person = Person(\"John\", \"Doe\", 25) print(repr(person))<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Person(\"John\",\"Doe\",25)<\/code><small>Code language: JavaScript (javascript)<\/small><\/code><\/pre>\n\n\n\n<p>If you execute the return string&nbsp;<code>Person(\"John\",\"Doe\",25)<\/code>, it\u2019ll return the&nbsp;<code>person<\/code>&nbsp;object.<\/p>\n\n\n\n<p>When a class doesn\u2019t implement the\u00a0<code>__str__<\/code>\u00a0method and you pass an instance of that class to the\u00a0<code>str()<\/code>, Python returns the result of the\u00a0<code>__repr__<\/code>\u00a0method because internally the\u00a0<code>__str__<\/code>\u00a0method calls the\u00a0<code>__repr__<\/code>\u00a0method:<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>person = Person('John', 'Doe', 25) print(person)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Person(\"John\",\"Doe\",25)<\/code><small>Code language: JavaScript (javascript)<\/small><\/code><\/pre>\n\n\n\n<p>If a class implements the\u00a0<code>__str__<\/code>\u00a0method, Python will call the\u00a0<code>__str__<\/code>\u00a0method when you pass an instance of the class to the\u00a0<code>str()<\/code>. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age def __repr__(self): return f'Person(\"{self.first_name}\",\"{self.last_name}\",{self.age})' def __str__(self): return f'({self.first_name},{self.last_name},{self.age})' person = Person('John', 'Doe', 25) <em># use str()<\/em> print(person) <em># use repr()<\/em> print(repr(person)) <\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>(John,Doe,25) Person(\"John\",\"Doe\",25)<\/code><small>Code language: JavaScript (javascript)<\/small><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><code>__str__<\/code>&nbsp;vs&nbsp;<code>__repr__<\/code><\/h2>\n\n\n\n<p>The main difference between&nbsp;<code>__str__<\/code>&nbsp;and&nbsp;<code>__repr__<\/code>&nbsp;method is intended audiences.<\/p>\n\n\n\n<p>The&nbsp;<code>__str__<\/code>&nbsp;method returns a string representation of an object that is human-readable while the&nbsp;<code>__repr__<\/code>&nbsp;method returns a string representation of an object that is machine-readable.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn how to use the Python&nbsp;__repr__&nbsp;dunder method and the difference between the&nbsp;__repr__&nbsp;and&nbsp;__str__&nbsp;methods. Introduction to the Python&nbsp;__repr__&nbsp;magic method The\u00a0__repr__\u00a0dunder method defines behavior when you pass an instance of a\u00a0class\u00a0to the\u00a0repr(). The&nbsp;__repr__&nbsp;method returns the string representation of an object. Typically, the&nbsp;__repr__()&nbsp;returns a string that can be executed and yield the same value [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"class_list":["post-154","post","type-post","status-publish","format-standard","hentry","category-2-special-methods"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/154","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=154"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/154\/revisions"}],"predecessor-version":[{"id":155,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/154\/revisions\/155"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}