{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Encapsulación\n", "Consiste en denegar el acceso a los atributos y métodos internos de la clase desde el exterior.\n", "\n", "En Python no existe, pero se puede simular precediendo atributos y métodos con dos barras bajas __:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class Ejemplo:\n", " __atributo_privado = \"Soy un atributo inalcanzable desde fuera\"\n", " \n", " def __metodo_privado(self):\n", " print(\"Soy un método inalcanzable desde fuera\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "e = Ejemplo()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "ename": "AttributeError", "evalue": "'Ejemplo' object has no attribute '__atributo_privado'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0me\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__atributo_privado\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mAttributeError\u001b[0m: 'Ejemplo' object has no attribute '__atributo_privado'" ] } ], "source": [ "e.__atributo_privado" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "ename": "AttributeError", "evalue": "'Ejemplo' object has no attribute '__metodo_privado'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0me\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__metodo_privado\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mAttributeError\u001b[0m: 'Ejemplo' object has no attribute '__metodo_privado'" ] } ], "source": [ "e.__metodo_privado()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Cómo acceder\n", "Internamente la clase sí puede acceder a sus atributos y métodos encapsulados, el truco consiste en crear sus equivalentes \"publicos\":" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class Ejemplo:\n", " __atributo_privado = \"Soy un atributo inalcanzable desde fuera\"\n", " \n", " def __metodo_privado(self):\n", " print(\"Soy un método inalcanzable desde fuera\")\n", " \n", " def atributo_publico(self):\n", " return self.__atributo_privado\n", " \n", " def metodo_publico(self):\n", " return self.__metodo_privado()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "e = Ejemplo()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'Soy un atributo inalcanzable desde fuera'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "e.atributo_publico()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Soy un método inalcanzable desde fuera\n" ] } ], "source": [ "e.metodo_publico()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.1" } }, "nbformat": 4, "nbformat_minor": 0 }