{"id":733,"date":"2024-04-14T11:01:38","date_gmt":"2024-04-14T11:01:38","guid":{"rendered":"https:\/\/javigomez.org\/?p=733"},"modified":"2025-09-29T20:33:15","modified_gmt":"2025-09-29T20:33:15","slug":"06-02-python-clases-__repr__-y-__str__","status":"publish","type":"post","link":"https:\/\/javigomez.org\/index.php\/2024\/04\/14\/06-02-python-clases-__repr__-y-__str__\/","title":{"rendered":"06.02 Python \u2013 Clases &#8211; __repr__ y __str__"},"content":{"rendered":"\n<p><strong>Redefinir el m\u00e9todo <\/strong><strong>repr<\/strong><strong>():<\/strong><\/p>\n\n\n\n<p>El m\u00e9todo __repr__ se llama cada vez que se imprime un objeto. Puedes redefinirlo en tu clase para controlar c\u00f3mo se muestra el objeto al imprimirlo. El m\u00e9todo debe retornar una cadena que represente el objeto.<\/p>\n\n\n\n<p><strong>M\u00e9todo str<\/strong><\/p>\n\n\n\n<p>El m\u00e9todo __str__ en Python tambi\u00e9n se utiliza para controlar c\u00f3mo se representa un objeto al momento de imprimirlo, pero a diferencia de __repr__, su objetivo es brindar una representaci\u00f3n m\u00e1s legible para el usuario.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Persona:\n    def __init__(self,n,e):\n        self.nombre=n\n        self.edad=e\n\n    def __repr__(self):\n        return f\"Nombre: {self.nombre} - Edad: {self.edad}\"\n\n    def __str__(self):\n        return f\"El nombre del cliente es {self.nombre} de {self.edad} a\u00f1os\"\n\np=Persona(\"Javi\",56)\n\nprint(p)\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"PYTHON U06 02 Clases repr str Attr de objetos\" width=\"1200\" height=\"900\" src=\"https:\/\/www.youtube.com\/embed\/skSGTie_woA?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Redefinir el m\u00e9todo repr(): El m\u00e9todo __repr__ se llama cada vez que se imprime un objeto. Puedes redefinirlo en tu clase para controlar c\u00f3mo se &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[14,15,79,277],"class_list":["post-733","post","type-post","status-publish","format-standard","hentry","category-python","tag-__repr__","tag-__str__","tag-clases","tag-python"],"_links":{"self":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/733","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/comments?post=733"}],"version-history":[{"count":1,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/733\/revisions"}],"predecessor-version":[{"id":861,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/733\/revisions\/861"}],"wp:attachment":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/media?parent=733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/categories?post=733"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/tags?post=733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}