D10d3

oop_cheatsheet-parent_child.py

Jan 24th, 2015
235
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import os
  2. os.system('cls')
  3. """ Note on how to be classy:
  4. class class(object):
  5. def method(self): #This function is called a "method" because it is inside of a class
  6. pass
  7. def method2(self): #the 'self' argument refers to whatever object called this method
  8. pass
  9.  
  10. first create an instance of the class:
  11. stuff = class()
  12. then call methods in the instance:
  13. stuff.method()
  14. """
  15.  
  16.  
  17. class Parent_class(object): #Parent_class object's features are implicit by Child_class
  18. hat = 0 #creates a hat variable an initializes it as 0
  19. name = "null"
  20. def method(self):
  21. print " Parent_class.method()"
  22. def implicit(self):
  23. print " Parent_class.implicit()"
  24. print " #Child_class does not have a 'implicit' method, defaults to Parent_class"
  25. def altered(self):
  26. print " Parent_class.altered()"
  27. def has_hat(self):
  28. hat = self.hat
  29. name = self.name
  30. if hat:
  31. print " %s has a hat!" % name
  32. else:
  33. print " %s is hatless!" % name
  34.  
  35. class Child_class(Parent_class): #Child_class objects features are overriden by Parent_class
  36. def method(self):
  37. print " Child_class.method()"
  38. print " #Child_class has a 'method()' supersedes Parent_class version"
  39. def altered(self):
  40. print " Child_class.altered()"
  41. print " #Child_class has a 'altered' method"
  42. print " super(Child_class, self).altered()"
  43. print " #supersedes child_class and calls Parent_class version of 'altered()'"
  44. super(Child_class, self).altered()
  45. print " Child_class.altered()"
  46. print " #Child_class version of 'altered()' continues after"
  47.  
  48. class display():
  49. def dad_instance(self):
  50. dad_instance.name = "dad_instance" #override the default 'name' setting of this instance
  51. print "display dad_instance of Parent_class(object) calls:"
  52.  
  53. print "dad_instance.method()"
  54. dad_instance.method()
  55.  
  56. print "dad_instance.implicit()"
  57. dad_instance.implicit()
  58.  
  59. print "dad_instance.altered()"
  60. dad_instance.altered()
  61. print""
  62. #print " dad_instance.has_hat() #first time with default variable"
  63. #print " dad_instance.has_hat() #second time, having overriden the variable"
  64. print ""
  65.  
  66.  
  67. def son_instance(self):
  68. son_instance.name = "son_instance" #override the default 'name' setting of this instance
  69. print "display son_instance of Child_class(Parent_class) calls"
  70.  
  71. print "'son_instance.method()' results in:"
  72. son_instance.method()
  73.  
  74. print "'son_instance.implicit()' results in:"
  75. son_instance.implicit()
  76.  
  77. print "'son_instance.altered()' results in:"
  78. son_instance.altered()
  79. print ""
  80.  
  81. def dads_hat(self):
  82. print "Dad's hat:"
  83. print "dad_instance.has_hat() #inherits the default of 0 and registers as hatless"
  84. dad_instance.has_hat()
  85. dad_instance.hat = 1
  86. print "dad_instance.hat = 1 #overrides the default hat setting of this instance"
  87. dad_instance.has_hat() #now overridden, dad_instance has a hat!
  88. print "dad_instance.has_hat() #now overridden, dad_instance has a hat!"
  89. print""
  90.  
  91. def sons_hat(self):
  92. print "Son's hat:"
  93. print "son_instance.has_hat() #son_instance doesn't have a hat value, inherits from Parent_class"
  94. son_instance.has_hat()
  95. print "son_instance.hat = 1 #overrides the default 'Parent_class' value with one specific to the 'son_instance' instance"
  96. son_instance.hat = 1 #overrides the default 'Parent_class' value with on specific to the 'son_instance' instance
  97. print "son_instance.has_hat() #again, now that the hat value is overridden"
  98. son_instance.has_hat()
  99. print""
  100.  
  101.  
  102. #instantiate classes: instance = class()
  103. dad_instance = Parent_class()
  104. son_instance = Child_class()
  105. show = display()
  106.  
  107. #call methods from 'show' instance of display() class
  108. show.dad_instance()
  109. show.son_instance()
  110. show.dads_hat()
  111. show.sons_hat()
RAW Paste Data