User Tools

Site Tools


python:about_python:python_inheritance_fundamentals

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
python:about_python:python_inheritance_fundamentals [2018/02/24 22:40] – created mithatpython: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 ''@property'' decorator.
 +  * 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 ''ClickerCounter'' called ''ClickUpDown''.+To demonstrate the use of inheritance in Python, we are going to create a specialized version of one of these:((Picture from: "Totty Clicker - Gadgets at Play.com (UK)." Play.com (UK): DVDs, Music CDs, MP3s, Video Games, Books, Electronics & Gadgets - Free Delivery. [[http://www.play.com/Gadgets/Gadgets/4-/11566684/Totty-Clicker/Product.html?ptsl=1&ob=Price&fb=0#]] (accessed January 25, 2011).  
 +)) \\ {{tottycounter.jpg?234|}}
  
-<img src="images/clickupdown.png" title="ClickUpDown heirarchy" alt="ClickUpDown heirarchy" />  +A standard ''ClickerCounter'' has two buttons: one for incrementing the count and another for resetting itWe are going to use inheritance to create a special kind of ''ClickerCounter'': one that has an additional button to decrement the counter. In terms of a Python model, a ''ClickUpDown'' is identical to a ''ClickerCounter'' except that it has an additional method: ''click_down''.
  
-In addtion to the two buttons found on a ''ClickerCounter'' (for incrementing and resetting the count), a ''ClickUpDown'' object will add another that resets the count. In terms of a Python model, a ''ClickUpDown'' is identical to a ''ClickerCounter'' except that it has an additional method''click_down''.+{{:python:about_python:clickupdown-tree.png}}
  
 Here is the base class: Here is the base class:
Line 96: Line 107:
 </code> </code>
  
-===== 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, the behavior of objects is based on the //class to which the object belongs//---not to the //class of the variable by which you are accessing the object//.+===== What you should do next =====
  
-===== Polymorphism in Python =====+This covers the very basics of inheritance in Python. To level up your use of inheritance in Python, you should next have a look at the following:
  
-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's class+  * Overriding methods
- +  * Derived class constructors (including calling parent class constructors).
-<code python> +
-a = ClickUpDown()       # a now references a ClickUpDown object +
-a = ClickerCounter()    # a now references a ClickerCounter object +
-</code> +
- +
-In fact, it's hard to make Python behave non-polymorphically. +
- +
-===== Parameterized constructor example ===== +
- +
-Let's now add a ''click_limit'' feature to our ''ClickerCounter'' When the user clicks past the ''click_limit'', the count will automatically reset to zero. By default, the ''click_limit'' will be 100,000. Let us also let the user set the ''click_limit'' when she or he instantiates a ''ClickerCounter''+
- +
-To do this, we will need to add an instance variable to store the ''click_limit'', add some logic to the ''click'' method, and add a parameterized constructor: +
- +
-<code python> +
-class ClickerCounter():         +
-    # parameterized constructor +
-    def __init__(self, click_limit = 100000): +
-        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 +
-</code> +
- +
-To use it: +
- +
-<code python> +
-a = ClickerCounter(3)       # make a clicker-counter that counts up to 3 +
-a.click() +
-a.click() +
-a.click() +
-print a.get_count()         # should be 3 +
-a.click()                   # should go from 3 to 0 +
-print a.get_count()         # should be 0 +
-</code>+
  
 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

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki