943,015 Members | Top Members by Rank

Ad:
  • Python Code Snippet
  • Views: 70351
  • Python RSS
0

A Simple Class Inheritance Example (Python)

by on Aug 11th, 2005
Python is entirely object oriented and using classes is made relatively simple. Beginners have a certain angst when it comes to using classes. There is a hump in the learning curve, which I like to overcome with this example. Inheritance really makes sense and saves you a lot of extra code writing. The best way to learn about classes and object oriented programming is to write more and more of your code with class!
Python Code Snippet (Toggle Plain Text)
  1. # a simple example of a class inheritance
  2. # tested with Python24 vegaseat 10aug2005
  3.  
  4. help('object') # test
  5.  
  6. class Class1(object):
  7. """
  8. Class1 inherits the most basic container class object (just a place holder)
  9. this is the newer class writing convention, adding (object) is "still" optional
  10. """
  11.  
  12. k = 7
  13.  
  14. def __init__(self, color='green'):
  15. """
  16. Special method __init__() is called first (acts as Constructor).
  17. It brings in data from outside the class like the variable color.
  18. (in this case color is also set to a default value of green)
  19. The first parameter of any method/function in the class is always self,
  20. the name self is used by convention. Assigning color to self.color allows it
  21. to be passed to all methods within the class. Think of self as a carrier,
  22. or if you want impress folks call it target instance object.
  23. The variable k is assigned a value in the class, but outside of the methods.
  24. You can access k in a method using self.k
  25. """
  26. self.color = color
  27.  
  28. def Hello1(self):
  29. print "Hello from Class1!"
  30.  
  31. def printColor(self):
  32. """in this case self allows color to be passed"""
  33. print "I like the color", self.color
  34.  
  35. def __localHello(self):
  36. """
  37. A variable or function with a double underline prefix and no or max. single
  38. underline postfix is considered private to the class and is not inherited or
  39. accessible outside the class.
  40. """
  41. print "A hardy Hello only used within the class!"
  42.  
  43.  
  44. class Class2(Class1):
  45. """
  46. Class2 inherits Class1 (Class2 is the subclass, Class1 the base or superclass)
  47. Class1 has to be coded before Class2 for this to work!!!
  48. Class2 can now use any method of Class1, and even the variable k
  49. """
  50.  
  51. def Hello2(self):
  52. print "Hello from Class2!"
  53. print self.k, "is my favorite number"
  54.  
  55.  
  56. # the color blue is passed to __init__()
  57. c1 = Class1('blue')
  58.  
  59. # Class2 inherited method __init__() from Class1
  60. # if you used c2 = Class2(), the default color green would be picked
  61. c2 = Class2('red')
  62.  
  63. print '-'*20
  64. print "Class1 says hello:"
  65. c1.Hello1()
  66.  
  67. print '-'*20
  68. print "Class2 says a Class1 hello:"
  69. c2.Hello1()
  70.  
  71. print '-'*20
  72. print "Class2 says its own hello:"
  73. c2.Hello2()
  74.  
  75. print '-'*20
  76. print "Class1 color via __init__():"
  77. c1.printColor()
  78.  
  79. print '-'*20
  80. print "Class2 color via inherited __init__() and printColor():"
  81. c2.printColor()
  82.  
  83. print '-'*20
  84. print "Class1 changes its mind about the color:"
  85. c1 = Class1('yellow') # same as: c1.__init__('yellow')
  86. c1.printColor()
  87.  
  88. print '-'*20
  89. print "Wonder what Class2 has to say now:"
  90. c2.printColor()
  91.  
  92. print '-'*20
  93. # this would give an error! Class1 does not have a method Hello2()
  94. if hasattr(Class1, "Hello2"):
  95. print c1.Hello2()
  96. else:
  97. print "Class1 does not contain method Hello2()"
  98.  
  99. # check inheritance
  100. if issubclass(Class2, Class1):
  101. print "Class2 is a subclass of Class1, or Class2 has inherited Class1"
  102.  
  103. # you can access variable k contained in Class1
  104. print "Variable k from Class1 =", c1.k
  105.  
  106. print '-'*20
  107. # this would give an error! You cannot access a class private method
  108. if hasattr(Class1, "__localHello()"):
  109. print c1.__localHello()
  110. else:
  111. print "No access to Class1 private method __localHello()"
Comments on this Code Snippet
Aug 25th, 2005
0

Re: A Simple Class Inheritance Example (Python)

Python has a leg up over Java. Python can have multiple inheritance, just separate the inherited classes with a comma.
DaniWeb's Hypocrite
vegaseat is offline Offline
5,789 posts
since Oct 2004
Feb 13th, 2008
0

Re: A Simple Class Inheritance Example (Python)

Python Syntax (Toggle Plain Text)
  1. You still can access __localHello() as _Class1__localHello()
Newbie Poster
edacval is offline Offline
1 posts
since Feb 2008
Aug 11th, 2010
-1

Re: A Simple Class Inheritance Example (Python)

A simple program @ http://fullchipdesign.com/python_calling_classes_inheritance.htm
Newbie Poster
atuli is offline Offline
1 posts
since Aug 2010
Aug 12th, 2010
0

Re: A Simple Class Inheritance Example (Python)

Starting with Python3
class Class1(object):
can be written
class Class1:
object is now automatically inherited.
DaniWeb's Hypocrite
vegaseat is offline Offline
5,789 posts
since Oct 2004
Message:
Previous Thread in Python Forum Timeline: Help with manipulating a string
Next Thread in Python Forum Timeline: Remove [] in list





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC