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

Both sides previous revisionPrevious revision
Next revision
Previous revision
python:about_python:python_inheritance_fundamentals [2018/02/24 23:02] – [What you'll learn] mithatpython:about_python:python_inheritance_fundamentals [2018/02/24 23:38] (current) – [What you'll need to know] mithat
Line 5: Line 5:
 ===== What you'll need to know ===== ===== What you'll need to know =====
  
-  * How to create a basic class definition.+  * How to create and use Python class.
   * How to use the ''@property'' decorator.   * How to use the ''@property'' decorator.
 +  * How to use parameterized constructors.
  
 ===== What you'll learn ===== ===== What you'll learn =====
  
-  * Inheritance key concept+  * Inheritance core concept
   * Generalization and specialization   * Generalization and specialization
   * Class heirarchies   * Class heirarchies
   * Inheritance and code reuse   * Inheritance and code reuse
   * Inheritance in Python   * Inheritance in Python
-  * Constructors and inheritance 
 ===== Inheritance ===== ===== Inheritance =====
  
Line 46: 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 =====
Line 110: Line 107:
 </code> </code>
  
-===== 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:+===== What you should do next =====
  
-<code python> +This covers the very basics of inheritance in PythonTo level up your use of inheritance in Python, you should next have a look at the following:
-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: +  * Overriding methods
- +  * Derived class constructors (including calling parent class constructors).
-<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.1519513323.txt.gz · Last modified: 2018/02/24 23:02 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki