Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
A first demonstration of OO, including polymorphism

"Object Oriented" often means big and heavy code even for the first example application demos ... since OO works really well when you're meeting requirements beyond those which are small enough to be described as 'trivial'. So I'm very pleased with this little demonstration in Python which shows - all in one file - the definition of four different classes, the building of a list of objects of various different sorts, and then a loop that goes through all the objects using 'polymorphism' to ensure that the right accessor code is run on the right piece of data.

Scenario - monthly bills need to be multiplied by 12 for the annual cost, Council tax bills come in 10 instalments, and annual payers get a 10% discount.

class monthly:
 def __init__(self,xxx):
  self.val = xxx
 
 def yearly(self):
  return self.val * 12
 
class annual:
 def __init__(self,xxx):
  self.val = xxx
 
 def yearly(self):
  return self.val * 0.9
 
class council:
 def __init__(self,xxx):
  self.val = xxx
 
 def yearly(self):
  return self.val * 10
 
# ----------------------------
 
david = monthly(25)
john = monthly(14)
gerry = annual(125)
lisa = council(112)
 
debtors = (david, john, gerry, lisa)
 
for debtor in debtors:
 print debtor.yearly()


Please note - this code is BELOW my minimum standards for a live application. I would expect to see comments, I would expect better variable naming, and I would use inheritance to save the need for a duplicated constructor.

Sample output:

92: grahamellis$ python accts
300
168
112.5
1120
92: grahamellis$

(written 2009-09-04, updated 2009-09-05)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Q906 - Object Orientation and General technical topics - Object Orientation: Individual Objects
  [4650] Why populate object with values as you construct them? - (2016-02-18)
  [4591] From single block to structure and object oriented programming - (2015-12-02)
  [4448] What is the difference between a function and a method? - (2015-03-04)
  [4021] Spike solution, refactored and reusable, Python - Example - (2013-02-28)
  [3721] Naming blocks of code, structures and Object Orientation - efficient coding in manageable chunks - (2012-05-06)
  [3436] Moving from scripting to Object Orientation in Python - (2011-09-13)
  [2651] Calculation within objects - early, last minute, or cached? - (2010-02-26)
  [2173] Basic OO principles - (2009-05-11)
  [2171] Cleaning up redundant objects - (2009-05-11)
  [1925] Introduction to Object Oriented Programming - (2008-12-06)
  [1864] Object Oriented Perl - First Steps - (2008-11-01)
  [1543] Learning Object Oriented Principles (and perhaps Java) - (2008-02-17)
  [507] Introduction to Object Oriented Programming - (2005-11-27)
  [227] Bellringing and Programming and Objects and Perl - (2005-02-25)


Back to
Lymington, New Forest - some pictures
Previous and next
or
Horse's mouth home
Forward to
Two days of demonstration scripts in Python
Some other Articles
Signwriting is dead. Long live the sign.
Easing off in our 50s?
From Lymington by train - last of the slammers
Two days of demonstration scripts in Python
A first demonstration of OO, including polymorphism
Lymington, New Forest - some pictures
Great Western Route Utilisation Strategy - Draft for Consultation
Dynamic / changing images on your web page
Writing with our customers words
`Of Course` is back!
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2393_A-f ... phism.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat