Custom Search

Wednesday, December 1, 2010

python Class Inheritance Example

python Class Inheritance Example
  1. # a simple example of a class inheritance
  2. # tested with Python24 vegaseat 10aug2005
  3. help('object') # test
  4. class Class1(object):
  5. """
  6. Class1 inherits the most basic container class object (just a place holder)
  7. this is the newer class writing convention, adding (object) is "still" optional
  8. """
  9. k = 7
  10. def __init__(self, color='green'):
  11. """
  12. Special method __init__() is called first (acts as Constructor).
  13. It brings in data from outside the class like the variable color.
  14. (in this case color is also set to a default value of green)
  15. The first parameter of any method/function in the class is always self,
  16. the name self is used by convention. Assigning color to self.color allows it
  17. to be passed to all methods within the class. Think of self as a carrier,
  18. or if you want impress folks call it target instance object.
  19. The variable k is assigned a value in the class, but outside of the methods.
  20. You can access k in a method using self.k
  21. """
  22. self.color = color
  23. def Hello1(self):
  24. print "Hello from Class1!"
  25. def printColor(self):
  26. """in this case self allows color to be passed"""
  27. print "I like the color", self.color
  28. def __localHello(self):
  29. """
  30. A variable or function with a double underline prefix and no or max. single
  31. underline postfix is considered private to the class and is not inherited or
  32. accessible outside the class.
  33. """
  34. print "A hardy Hello only used within the class!"
  35. class Class2(Class1):
  36. """
  37. Class2 inherits Class1 (Class2 is the subclass, Class1 the base or superclass)
  38. Class1 has to be coded before Class2 for this to work!!!
  39. Class2 can now use any method of Class1, and even the variable k
  40. """
  41. def Hello2(self):
  42. print "Hello from Class2!"
  43. print self.k, "is my favorite number"
  44. # the color blue is passed to __init__()
  45. c1 = Class1('blue')
  46. # Class2 inherited method __init__() from Class1
  47. # if you used c2 = Class2(), the default color green would be picked
  48. c2 = Class2('red')
  49. print '-'*20
  50. print "Class1 says hello:"
  51. c1.Hello1()
  52. print '-'*20
  53. print "Class2 says a Class1 hello:"
  54. c2.Hello1()
  55. print '-'*20
  56. print "Class2 says its own hello:"
  57. c2.Hello2()
  58. print '-'*20
  59. print "Class1 color via __init__():"
  60. c1.printColor()
  61. print '-'*20
  62. print "Class2 color via inherited __init__() and printColor():"
  63. c2.printColor()
  64. print '-'*20
  65. print "Class1 changes its mind about the color:"
  66. c1 = Class1('yellow') # same as: c1.__init__('yellow')
  67. c1.printColor()
  68. print '-'*20
  69. print "Wonder what Class2 has to say now:"
  70. c2.printColor()
  71. print '-'*20
  72. # this would give an error! Class1 does not have a method Hello2()
  73. if hasattr(Class1, "Hello2"):
  74. print c1.Hello2()
  75. else:
  76. print "Class1 does not contain method Hello2()"
  77. # check inheritance
  78. if issubclass(Class2, Class1):
  79. print "Class2 is a subclass of Class1, or Class2 has inherited Class1"
  80. # you can access variable k contained in Class1
  81. print "Variable k from Class1 =", c1.k
  82. print '-'*20
  83. # this would give an error! You cannot access a class private method
  84. if hasattr(Class1, "__localHello()"):
  85. print c1.__localHello()
  86. else:
  87. print "No access to Class1 private method __localHello()"

No comments:

Post a Comment