Skip to content

Commit

Permalink
added polymorphism to OOP
Browse files Browse the repository at this point in the history
  • Loading branch information
TiVentures committed Feb 12, 2018
1 parent 6bdd11b commit a1bc17f
Show file tree
Hide file tree
Showing 2 changed files with 332 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,170 @@
"Finally, the derived class extends the functionality of the base class, by defining a new bark() method."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Polymorphism\n",
"\n",
"We've learned that while functions can take in different arguments, methods belong to the objects they act on. In Python, *polymorphism* refers to the way in which different object classes can share the same method name, and those methods can be called from the same place even though a variety of different objects might be passed in. The best way to explain this is by example:"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Niko says Woof!\n",
"Felix says Meow!\n"
]
}
],
"source": [
"class Dog:\n",
" def __init__(self, name):\n",
" self.name = name\n",
"\n",
" def speak(self):\n",
" return self.name+' says Woof!'\n",
" \n",
"class Cat:\n",
" def __init__(self, name):\n",
" self.name = name\n",
"\n",
" def speak(self):\n",
" return self.name+' says Meow!' \n",
" \n",
"niko = Dog('Niko')\n",
"felix = Cat('Felix')\n",
"\n",
"print(niko.speak())\n",
"print(felix.speak())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here we have a Dog class and a Cat class, and each has a `.speak()` method. When called, each object's `.speak()` method returns a result unique to the object.\n",
"\n",
"There a few different ways to demonstrate polymorphism. First, with a for loop:"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Niko says Woof!\n",
"Felix says Meow!\n"
]
}
],
"source": [
"for pet in [niko,felix]:\n",
" print(pet.speak())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Another is with functions:"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Niko says Woof!\n",
"Felix says Meow!\n"
]
}
],
"source": [
"def pet_speak(pet):\n",
" print(pet.speak())\n",
"\n",
"pet_speak(niko)\n",
"pet_speak(felix)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"In both cases we were able to pass in different object types, and we obtained object-specific results from the same mechanism.\n",
"\n",
"A more common practice is to use abstract classes and inheritance. An abstract class is one that never expects to be instantiated. For example, we will never have an Animal object, only Dog and Cat objects, although Dogs and Cats are derived from Animals:"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Fido says Woof!\n",
"Isis says Meow!\n"
]
}
],
"source": [
"class Animal:\n",
" def __init__(self, name): # Constructor of the class\n",
" self.name = name\n",
"\n",
" def talk(self): # Abstract method, defined by convention only\n",
" raise NotImplementedError(\"Subclass must implement abstract method\")\n",
"\n",
"\n",
"class Dog(Animal):\n",
" \n",
" def speak(self):\n",
" return self.name+' says Woof!'\n",
" \n",
"class Cat(Animal):\n",
"\n",
" def speak(self):\n",
" return self.name+' says Meow!'\n",
" \n",
"fido = Dog('Fido')\n",
"isis = Cat('Isis')\n",
"\n",
"print(fido.speak())\n",
"print(isis.speak())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Real life examples of polymorphism include:\n",
"* opening different file types - different tools are needed to display Word, pdf and Excel files\n",
"* adding different objects - the `+` operator performs arithmetic and concatenation"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -536,7 +700,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -559,7 +723,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 24,
"metadata": {},
"outputs": [
{
Expand Down
168 changes: 166 additions & 2 deletions 05-Object Oriented Programming/01-Object Oriented Programming.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,170 @@
"Finally, the derived class extends the functionality of the base class, by defining a new bark() method."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Polymorphism\n",
"\n",
"We've learned that while functions can take in different arguments, methods belong to the objects they act on. In Python, *polymorphism* refers to the way in which different object classes can share the same method name, and those methods can be called from the same place even though a variety of different objects might be passed in. The best way to explain this is by example:"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Niko says Woof!\n",
"Felix says Meow!\n"
]
}
],
"source": [
"class Dog:\n",
" def __init__(self, name):\n",
" self.name = name\n",
"\n",
" def speak(self):\n",
" return self.name+' says Woof!'\n",
" \n",
"class Cat:\n",
" def __init__(self, name):\n",
" self.name = name\n",
"\n",
" def speak(self):\n",
" return self.name+' says Meow!' \n",
" \n",
"niko = Dog('Niko')\n",
"felix = Cat('Felix')\n",
"\n",
"print(niko.speak())\n",
"print(felix.speak())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here we have a Dog class and a Cat class, and each has a `.speak()` method. When called, each object's `.speak()` method returns a result unique to the object.\n",
"\n",
"There a few different ways to demonstrate polymorphism. First, with a for loop:"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Niko says Woof!\n",
"Felix says Meow!\n"
]
}
],
"source": [
"for pet in [niko,felix]:\n",
" print(pet.speak())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Another is with functions:"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Niko says Woof!\n",
"Felix says Meow!\n"
]
}
],
"source": [
"def pet_speak(pet):\n",
" print(pet.speak())\n",
"\n",
"pet_speak(niko)\n",
"pet_speak(felix)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"In both cases we were able to pass in different object types, and we obtained object-specific results from the same mechanism.\n",
"\n",
"A more common practice is to use abstract classes and inheritance. An abstract class is one that never expects to be instantiated. For example, we will never have an Animal object, only Dog and Cat objects, although Dogs and Cats are derived from Animals:"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Fido says Woof!\n",
"Isis says Meow!\n"
]
}
],
"source": [
"class Animal:\n",
" def __init__(self, name): # Constructor of the class\n",
" self.name = name\n",
"\n",
" def talk(self): # Abstract method, defined by convention only\n",
" raise NotImplementedError(\"Subclass must implement abstract method\")\n",
"\n",
"\n",
"class Dog(Animal):\n",
" \n",
" def speak(self):\n",
" return self.name+' says Woof!'\n",
" \n",
"class Cat(Animal):\n",
"\n",
" def speak(self):\n",
" return self.name+' says Meow!'\n",
" \n",
"fido = Dog('Fido')\n",
"isis = Cat('Isis')\n",
"\n",
"print(fido.speak())\n",
"print(isis.speak())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Real life examples of polymorphism include:\n",
"* opening different file types - different tools are needed to display Word, pdf and Excel files\n",
"* adding different objects - the `+` operator performs arithmetic and concatenation"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -536,7 +700,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -559,7 +723,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 24,
"metadata": {},
"outputs": [
{
Expand Down

0 comments on commit a1bc17f

Please sign in to comment.