Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Inheritance

In (Object Oriented) programming terms, "Inheritance" is used to define a type of data (an "object") which behaves like another type of data with some additions and differences.

For example, we can define a type of data that's a piece of clothing. We can then define a pair of shoes which is like a piece of clothing, except that it has two extra pieces of information involved - a length and a width. Languages that are known as Object Oriented languages provide facilities to make such definitions easy without repeating code.

In the following piece of code (in Python), we define a piece of clothing (it's known as a base class) and then we extend it into a new class (called a subclass) which has the same code and properties as a piece of clothing, with the exception of changes that we list.

The tshirt and tights classes are also subclasses of clothing and we can make up a list of pieces of clothing (you'll see it done in the test code at the bottom) which we can then loop through reporting colour and size. As we loop through in this way, the getcolour method is INHERITED from the clothes base class in each case (i.e. it is common code) and the getsize method uses what we call POLYMORPHISM as it uses different code to return the size for different types of clothing.

class clothing:
  def __init__(self,colour,weight):
    self.colour=colour
    self.weight=weight
  def getcolour(self):
    return self.colour
  def getsize(self):
    return "n/a"
class shoes(clothing):
  def __init__(self,colour,weight,len,wid):
    clothing.__init__(self,colour,weight)
    self.length = len
    self.width = wid
  def getsize(self):
    r = "Length "+str(self.length)+" and width "+self.width
    return r
class tshirt(clothing):
  def __init__(self,colour,weight,size):
    clothing.__init__(self,colour,weight)
    self.size = size
  def getsize(self):
    return self.size
class tights(clothing):
  pass

if __name__ == "__main__":
  wearing = []
  wearing.append(tshirt("blue",1.0,"XL"))
  wearing.append(tights("pink",0.1))
  wearing.append(shoes("brown",2.0,11,"D"))
  for haveon in wearing:
    print "I am wearing something",haveon.getcolour(),
    print " size ",haveon.getsize()

Let's run that ...

[localhost:~/ipy] graham% python dresser
I am wearing something blue size XL
I am wearing something pink size n/a
I am wearing something brown size Length 11 and width D
[localhost:~/ipy] graham%


See also further examples and Python object training

Please note that articles in this section of our web site were current and correct to the best of our ability when published, but by the nature of our business may go out of date quite quickly. The quoting of a price, contract term or any other information in this area of our website is NOT an offer to supply now on those terms - please check back via our main web site

Related Material

Python - Objects - Intermediate
  [296] - ()
  [383] - ()
  [477] - ()
  [656] - ()
  [831] - ()
  [903] - ()
  [964] - ()
  [1146] - ()
  [1217] - ()
  [1517] - ()
  [1644] - ()
  [1661] - ()
  [1819] - ()
  [2368] - ()
  [2409] - ()
  [2485] - ()
  [2693] - ()
  [2717] - ()
  [2720] - ()
  [2722] - ()
  [2764] - ()
  [2785] - ()
  [2889] - ()
  [2905] - ()
  [2994] - ()
  [3002] - ()
  [3442] - ()
  [3472] - ()
  [3524] - ()
  [3796] - ()
  [3887] - ()
  [4028] - ()
  [4094] - ()
  [4344] - ()
  [4356] - ()
  [4366] - ()
  [4410] - ()
  [4449] - ()
  [4450] - ()
  [4541] - ()
  [4649] - ()
  [4717] - ()
  [4718] - ()
  [4719] - ()

Object Orientation and General technical topics - Object Orientation: Composite Objects
  [477] - ()
  [592] - ()
  [1345] - ()
  [1348] - ()
  [2170] - ()
  [2641] - ()
  [2865] - ()
  [2922] - ()
  [3142] - ()
  [3152] - ()
  [3251] - ()
  [3609] - ()
  [3979] - ()
  [4377] - ()
  [4394] - ()
  [4450] - ()

resource index - Python
Solutions centre home page

You'll find shorter technical items at The Horse's Mouth and delegate's questions answered at the Opentalk forum.

At Well House Consultants, we provide training courses on subjects such as Ruby, Lua, Perl, Python, Linux, C, C++, Tcl/Tk, Tomcat, PHP and MySQL. We're asked (and answer) many questions, and answers to those which are of general interest are published in this area of our site.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.info/solutions/python-i ... tance.html • PAGE BUILT: Wed Mar 28 07:47:11 2012 • BUILD SYSTEM: wizard