{"id":739,"date":"2024-04-14T11:08:30","date_gmt":"2024-04-14T11:08:30","guid":{"rendered":"https:\/\/javigomez.org\/?p=739"},"modified":"2025-09-29T20:33:15","modified_gmt":"2025-09-29T20:33:15","slug":"06-04-python-clases-getter-y-setter","status":"publish","type":"post","link":"https:\/\/javigomez.org\/index.php\/2024\/04\/14\/06-04-python-clases-getter-y-setter\/","title":{"rendered":"06.05 Python \u2013 Clases &#8211; Getter y Setter"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>class Persona:\n    def __init__(self, nombre, edad):\n        self._nombre = nombre\n        self._edad = edad\n\n    @property\n    def nombre(self):\n        print(\"Obteniendo el nombre...\")\n        return self._nombre\n\n    @nombre.setter\n    def nombre(self, nuevo_nombre):\n        print(\"Estableciendo el nombre...\")\n        self._nombre = nuevo_nombre\n\n    @property\n    def edad(self):\n        print(\"Obteniendo la edad...\")\n        return self._edad\n\n    @edad.setter\n    def edad(self, nueva_edad):\n        print(\"Estableciendo la edad...\")\n        if nueva_edad >= 0:\n            self._edad = nueva_edad\n        else:\n            print(\"La edad no puede ser un n\u00famero negativo.\")\n\n# Crear un objeto de la clase Persona\npersona1 = Persona(\"Juan\", 30)\n\n# Acceder al atributo nombre utilizando el m\u00e9todo get\nprint(persona1.nombre)  # Resultado: \"Obteniendo el nombre...\" \"Juan\"\n\n# Establecer un nuevo valor para el atributo nombre utilizando el m\u00e9todo set\npersona1.nombre = \"Pedro\"  # Resultado: \"Estableciendo el nombre...\"\n\n# Intentar establecer una edad negativa\npersona1.edad = -5  # Resultado: \"Estableciendo la edad...\" \"La edad no puede ser un n\u00famero negativo.\u201c\n\npersona1._edad = -4\nprint(f'Edad: {persona1._edad}')  #Edad: -4<\/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 05 Clases getter setter\" width=\"1200\" height=\"900\" src=\"https:\/\/www.youtube.com\/embed\/I8UfIc_6wpU?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":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[79,174,277,305],"class_list":["post-739","post","type-post","status-publish","format-standard","hentry","category-python","tag-clases","tag-getter","tag-python","tag-setter"],"_links":{"self":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/739","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=739"}],"version-history":[{"count":1,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/739\/revisions"}],"predecessor-version":[{"id":858,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/739\/revisions\/858"}],"wp:attachment":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/media?parent=739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/categories?post=739"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/tags?post=739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}