"## 1.5 Programacion Orientada a Objetos: Clases "
"## 3.1 Clases \n",
"\n",
"Las clases proporcionan un medio de agrupar datos y funcionalidad. La creación de una nueva clase crea un nuevo tipo de objeto, lo que permite crear nuevas instancias de ese objeto. Cada instancia de la clase puede tener atributos adjuntos para mantener su estado. Las instancias de clase también pueden tener métodos (definidos por su clase) para modificar su estado."
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
"class Dog:\n",
" tricks = [] # Variable de clase\n",
" kind = 'canine' # Variable de clase\n",
" def __init__(self, name):\n",
" self.name = name # Variable de instancia\n",
" def add_trick(self, trick):\n",
" self.tricks.append(trick)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'canine'"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Dog.kind\n"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"ename": "AttributeError",
"evalue": "type object 'Dog' has no attribute 'name'",
"\u001b[0;32m<ipython-input-30-629675d46941>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mDog\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m: type object 'Dog' has no attribute 'name'"
]
}
],
"source": [
"Dog.name"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Max'"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Dog(\"Max\").name"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"Max = Dog(\"Max\")\n",
"Keeper = Dog(\"Keeper\")"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'canine'"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Max.kind"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'canine'"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Max.kind=\"felis\"\n",
"Keeper.kind"
]
]
},
},
{
{
...
@@ -13,15 +142,44 @@
...
@@ -13,15 +142,44 @@
"execution_count": null,
"execution_count": null,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": []
"source": [
"Max. def add_trick(self, trick):\n",
" self.tricks.append(trick)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3.2 Herencia"
]
},
},
{
{
"cell_type": "markdown",
"cell_type": "markdown",
"metadata": {},
"metadata": {},
"source": [
"source": [
"## 1.6 Programacion Orientada a Objetos: Herencia y Polimorfismo"
"\u001b[0;32m<ipython-input-12-afcb87a7ff57>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mhelp\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdict_items\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mNameError\u001b[0m: name 'dict_items' is not defined"
"## 1.5 Programacion Orientada a Objetos: Clases "
"## 3.1 Clases \n",
"\n",
"Las clases proporcionan un medio de agrupar datos y funcionalidad. La creación de una nueva clase crea un nuevo tipo de objeto, lo que permite crear nuevas instancias de ese objeto. Cada instancia de la clase puede tener atributos adjuntos para mantener su estado. Las instancias de clase también pueden tener métodos (definidos por su clase) para modificar su estado."
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
"class Dog:\n",
" tricks = [] # Variable de clase\n",
" kind = 'canine' # Variable de clase\n",
" def __init__(self, name):\n",
" self.name = name # Variable de instancia\n",
" def add_trick(self, trick):\n",
" self.tricks.append(trick)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'canine'"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Dog.kind\n"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"ename": "AttributeError",
"evalue": "type object 'Dog' has no attribute 'name'",
"\u001b[0;32m<ipython-input-30-629675d46941>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mDog\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m: type object 'Dog' has no attribute 'name'"
]
}
],
"source": [
"Dog.name"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Max'"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Dog(\"Max\").name"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"Max = Dog(\"Max\")\n",
"Keeper = Dog(\"Keeper\")"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'canine'"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Max.kind"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'canine'"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Max.kind=\"felis\"\n",
"Keeper.kind"
]
]
},
},
{
{
...
@@ -13,15 +142,44 @@
...
@@ -13,15 +142,44 @@
"execution_count": null,
"execution_count": null,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": []
"source": [
"Max. def add_trick(self, trick):\n",
" self.tricks.append(trick)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3.2 Herencia"
]
},
},
{
{
"cell_type": "markdown",
"cell_type": "markdown",
"metadata": {},
"metadata": {},
"source": [
"source": [
"## 1.6 Programacion Orientada a Objetos: Herencia y Polimorfismo"
"\u001b[0;32m<ipython-input-12-afcb87a7ff57>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mhelp\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdict_items\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mNameError\u001b[0m: name 'dict_items' is not defined"