python:about_python:python_inheritance_fundamentals
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
python:about_python:python_inheritance_fundamentals [2018/02/24 22:40] – created mithat | python:about_python:python_inheritance_fundamentals [2018/02/24 23:38] (current) – [What you'll need to know] mithat | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Python Inheritance Fundamentals ====== | ====== Python Inheritance Fundamentals ====== | ||
- | Any language that supports object orientation will let you create objects, either by instantiating them from classes or by building them from prototypes. Class-based object orientation becomes significantly more powerful when you are able to use **inheritance**. Python permits this. | + | Any language that supports object orientation will let you create objects, either by instantiating them from classes or by building them from prototypes. Class-based object orientation becomes significantly more powerful when you are able to use **inheritance**. |
+ | ===== What you'll need to know ===== | ||
+ | |||
+ | * How to create and use a Python class. | ||
+ | * How to use the '' | ||
+ | * How to use parameterized constructors. | ||
+ | |||
+ | ===== What you'll learn ===== | ||
+ | |||
+ | * Inheritance core concept | ||
+ | * Generalization and specialization | ||
+ | * Class heirarchies | ||
+ | * Inheritance and code reuse | ||
+ | * Inheritance in Python | ||
===== Inheritance ===== | ===== Inheritance ===== | ||
Line 33: | Line 46: | ||
Since you do not need to rewrite the code that is common to both base and derived classes, you end up writing less code. But even more important, when you fix a bug in the base class, it automatically propagates to the derived classes. | Since you do not need to rewrite the code that is common to both base and derived classes, you end up writing less code. But even more important, when you fix a bug in the base class, it automatically propagates to the derived classes. | ||
- | ==== Inheritance and polymorphism ==== | ||
- | |||
- | Another advantage of using inheritance is that with statically typed languages (such as C++ and Java), it facilitates **polymorphism** (discussed below). Dynamically typed languages (like Python) behave polymorphically almost by definition. | ||
===== Inheritance in Python ===== | ===== Inheritance in Python ===== | ||
- | To demonstrate the use of inheritance in Python, we are going to create a specialized version of '' | + | To demonstrate the use of inheritance in Python, we are going to create a specialized version of one of these: |
+ | )) \\ {{tottycounter.jpg? | ||
- | <img src=" | + | A standard '' |
- | In addtion to the two buttons found on a '' | + | {{: |
Here is the base class: | Here is the base class: | ||
Line 96: | Line 107: | ||
</ | </ | ||
- | ===== Polymorphism ===== | ||
- | In statically typed programming languages, the behavior of an object bound to a variable might be: \\ | ||
- | a) the behavior associated with the class of the variable used to reference the object, or \\ | ||
- | b) the behavior associated with the class of the object to which the variable is bound. | ||
- | We call b) above **polymorphism**. In other words, when a language behaves polymorphically, | + | ===== What you should do next ===== |
- | ===== Polymorphism | + | This covers the very basics of inheritance |
- | Because Python is dynamically typed, Python is polymorphic without any extra effort on the part of the programmer. When variables are bound to objects, the type of the variable changes---therefore the behavior is always determined by the object' | + | * Overriding methods. |
- | + | | |
- | <code python> | + | |
- | a = ClickUpDown() | + | |
- | a = ClickerCounter() | + | |
- | </ | + | |
- | + | ||
- | In fact, it's hard to make Python behave non-polymorphically. | + | |
- | + | ||
- | ===== Parameterized constructor example ===== | + | |
- | + | ||
- | Let's now add a '' | + | |
- | + | ||
- | To do this, we will need to add an instance variable to store the '' | + | |
- | + | ||
- | <code python> | + | |
- | class ClickerCounter(): | + | |
- | # parameterized constructor | + | |
- | def __init__(self, | + | |
- | self.count = 0 | + | |
- | self.click_limit = click_limit | + | |
- | + | ||
- | # accessor for count | + | |
- | def get_count(self): | + | |
- | return self.count | + | |
- | + | ||
- | # click the counter | + | |
- | def click(self): | + | |
- | if self.count < self.click_limit: | + | |
- | self.count = self.count + 1 | + | |
- | else: | + | |
- | self.reset() | + | |
- | + | ||
- | # reset the count | + | |
- | def reset(self): | + | |
- | self.count = 0 | + | |
- | </ | + | |
- | + | ||
- | To use it: | + | |
- | + | ||
- | <code python> | + | |
- | a = ClickerCounter(3) | + | |
- | a.click() | + | |
- | a.click() | + | |
- | a.click() | + | |
- | print a.get_count() | + | |
- | a.click() | + | |
- | print a.get_count() | + | |
- | </ | + | |
Copyright © 2011-2018 Mithat Konar. All rights reserved. | Copyright © 2011-2018 Mithat Konar. All rights reserved. |
python/about_python/python_inheritance_fundamentals.1519512022.txt.gz · Last modified: 2018/02/24 22:40 by mithat