Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- os.system('cls')
- """ Note on how to be classy:
- class class(object):
- def method(self): #This function is called a "method" because it is inside of a class
- pass
- def method2(self): #the 'self' argument refers to whatever object called this method
- pass
- first create an instance of the class:
- stuff = class()
- then call methods in the instance:
- stuff.method()
- """
- class Parent_class(object): #Parent_class object's features are implicit by Child_class
- hat = 0 #creates a hat variable an initializes it as 0
- name = "null"
- def method(self):
- print " Parent_class.method()"
- def implicit(self):
- print " Parent_class.implicit()"
- print " #Child_class does not have a 'implicit' method, defaults to Parent_class"
- def altered(self):
- print " Parent_class.altered()"
- def has_hat(self):
- hat = self.hat
- name = self.name
- if hat:
- print " %s has a hat!" % name
- else:
- print " %s is hatless!" % name
- class Child_class(Parent_class): #Child_class objects features are overriden by Parent_class
- def method(self):
- print " Child_class.method()"
- print " #Child_class has a 'method()' supersedes Parent_class version"
- def altered(self):
- print " Child_class.altered()"
- print " #Child_class has a 'altered' method"
- print " super(Child_class, self).altered()"
- print " #supersedes child_class and calls Parent_class version of 'altered()'"
- super(Child_class, self).altered()
- print " Child_class.altered()"
- print " #Child_class version of 'altered()' continues after"
- class display():
- def dad_instance(self):
- dad_instance.name = "dad_instance" #override the default 'name' setting of this instance
- print "display dad_instance of Parent_class(object) calls:"
- print "dad_instance.method()"
- dad_instance.method()
- print "dad_instance.implicit()"
- dad_instance.implicit()
- print "dad_instance.altered()"
- dad_instance.altered()
- print""
- #print " dad_instance.has_hat() #first time with default variable"
- #print " dad_instance.has_hat() #second time, having overriden the variable"
- print ""
- def son_instance(self):
- son_instance.name = "son_instance" #override the default 'name' setting of this instance
- print "display son_instance of Child_class(Parent_class) calls"
- print "'son_instance.method()' results in:"
- son_instance.method()
- print "'son_instance.implicit()' results in:"
- son_instance.implicit()
- print "'son_instance.altered()' results in:"
- son_instance.altered()
- print ""
- def dads_hat(self):
- print "Dad's hat:"
- print "dad_instance.has_hat() #inherits the default of 0 and registers as hatless"
- dad_instance.has_hat()
- dad_instance.hat = 1
- print "dad_instance.hat = 1 #overrides the default hat setting of this instance"
- dad_instance.has_hat() #now overridden, dad_instance has a hat!
- print "dad_instance.has_hat() #now overridden, dad_instance has a hat!"
- print""
- def sons_hat(self):
- print "Son's hat:"
- print "son_instance.has_hat() #son_instance doesn't have a hat value, inherits from Parent_class"
- son_instance.has_hat()
- print "son_instance.hat = 1 #overrides the default 'Parent_class' value with one specific to the 'son_instance' instance"
- son_instance.hat = 1 #overrides the default 'Parent_class' value with on specific to the 'son_instance' instance
- print "son_instance.has_hat() #again, now that the hat value is overridden"
- son_instance.has_hat()
- print""
- #instantiate classes: instance = class()
- dad_instance = Parent_class()
- son_instance = Child_class()
- show = display()
- #call methods from 'show' instance of display() class
- show.dad_instance()
- show.son_instance()
- show.dads_hat()
- show.sons_hat()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement