Advertisement
KChrisC

Python Class Inheritance Examples

Jun 4th, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.64 KB | None | 0 0
  1. #-------------------------------------------------------------------------------
  2. # Name:        ClassAndInheritance.py
  3. # Purpose:     Study class inheritance
  4. #
  5. # Notes:
  6. #
  7. # References:  http://www.jesshamrick.com/2011/05/18/an-introduction-to-classes-and-inheritance-in-python/
  8. #
  9. # Author:      User
  10. #
  11. # Created:     04/06/2014
  12. # Copyright:   (c) User 2014
  13. # Licence:     <your licence>
  14. #-------------------------------------------------------------------------------
  15.     # Imports #
  16. import locale
  17. from datetime import datetime
  18.  
  19. # Configurations #
  20. locale.setlocale(locale.LC_ALL, '') # Set the locale for your system 'en_US.UTF-8'
  21.  
  22.  
  23.     # Main #
  24. def main():
  25.         # Variables
  26.             # Global
  27.  
  28.             # Local
  29.  
  30.         # Main Code
  31.     o_PetPolly = c_Pet("Polly", "Parrot") ## Calls main class to create this instance
  32.     o_PetGinger = sc_Cat("Ginger", "True") ## Calls 'Cat" subclass with name and 'hates dogs' disposition
  33.     o_PetClifford = sc_Dog("Clifford", "True") ## Calls 'Dog" subclass with name and 'chases cats' disposition
  34.  
  35.     print("Going to about a main class created instance")
  36.     print(o_PetPolly)
  37.     print("Typical way to call a method, 'o_PetPolly.getName()': %s") %(o_PetPolly.getName())
  38.     print("Less typical way to call a method, 'Pet.getName(o_PetPolly)': %s") %(c_Pet.getName(o_PetPolly))
  39.     print("Done.")
  40.     print("")
  41.  
  42.     print("More about main class created instance") %()
  43.     print("Obj name: %s") %(o_PetPolly.getName())
  44.     print("Obj species: %s") %(o_PetPolly.getSpecies())
  45.     print(o_PetPolly)
  46.     print("")
  47.     print("About this subclass created instance") %()
  48.     print("Obj name: %s") %(o_PetGinger.getName())
  49.     print("Obj species: %s") %(o_PetGinger.getSpecies())
  50.     print("Obj hates dogs?: %s") %(o_PetGinger.hatesDogs())
  51.     print(o_PetGinger)
  52.     print("")
  53.     print("And about this subclass created instance") %()
  54.     print("Obj name: %s") %(o_PetClifford.getName())
  55.     print("Obj species: %s") %(o_PetClifford.getSpecies())
  56.     print("Obj chase cats?: %s") %(o_PetClifford.chasesCats())
  57.     print(o_PetClifford)
  58.     print("")
  59.  
  60.     print("Instance checks showing class and inheritance")
  61.     mister_pet = c_Pet("Mister", "Dog")
  62.     mister_dog = sc_Dog("Mister", True)
  63.     print("Is mister_pet an instance of c_Pet?: %s") %(isinstance(mister_pet, c_Pet))
  64.     print("Is mister_pet an instance of sc_Dog?: %s") %(isinstance(mister_pet, sc_Dog)) ## Not part of 'dog' as created via main class
  65.     print("Is mister_dog an instance of c_Pet?: %s") %(isinstance(mister_dog, c_Pet)) ## Yes as came in via subclass 'dog'
  66.     print("Is mister_dog an instance of sc_Dog?: %s") %(isinstance(mister_dog, sc_Dog))## Yes as came in via subclass 'dog'
  67.     print("")
  68.  
  69.     print("Some pet instance facts.")
  70.     o_Fido = sc_Dog("Fido", True) ## (name, chase cats?)
  71.     o_Rover = sc_Dog("Rover", False) ## (name, chase cats?)
  72.     o_Mittens = sc_Cat("Mittens", True) ## (name, hates dogs?)
  73.     o_Fluffy = sc_Cat("Fluffy", False) ## (name, hates dogs?)
  74.     print("I am: %s") %(o_Fido)
  75.     print("I am: %s") %(o_Rover)
  76.     print("I am: %s") %(o_Mittens)
  77.     print("I am: %s") %(o_Fluffy)
  78.     print("")
  79.     print "%s chases cats: %s" %(o_Fido.getName(), o_Fido.chasesCats())
  80.     print "%s chases cats: %s" %(o_Rover.getName(), o_Rover.chasesCats())
  81.     print "%s hates dogs: %s" % (o_Mittens.getName(), o_Mittens.hatesDogs())
  82.     print "%s hates dogs: %s" % (o_Fluffy.getName(), o_Fluffy.hatesDogs())
  83.  
  84.  
  85.  
  86.         # Tests
  87.             # Output to file
  88.     ## v_Path = 'C:/Users/User/Documents/out.txt'
  89.     ## v_Mode = 'w'
  90.     ## v_FileConn = f_OutputToFileOut(v_Path, v_Mode)
  91.  
  92.     ## sys.exit("Error message") ## Stop execution
  93.  
  94. #------------------------------
  95.  
  96.     # Functions #
  97.         # Normal Functions
  98.  
  99.         # Generator Functions
  100.  
  101.         # Lambda Functions
  102.  
  103.     # Classes #
  104. class c_Pet(object):
  105.     def __init__(self, name, species):
  106.         self.name = name
  107.         self.species = species
  108.     def getName(self):
  109.         return self.name
  110.     def getSpecies(self):
  111.         return self.species
  112.     def __str__(self):
  113.         return "%s is a %s" % (self.name, self.species)
  114.  
  115. class sc_Dog(c_Pet): ## Subclass of c_Pet
  116.     def __init__(self, name, chases_cats):
  117.         c_Pet.__init__(self, name, "Dog")
  118.         self.chases_cats = chases_cats
  119.     def chasesCats(self):
  120.         return self.chases_cats
  121.  
  122. class sc_Cat(c_Pet): ## Subclass of c_Pet
  123.     def __init__(self, name, hates_dogs):
  124.         c_Pet.__init__(self, name, "Cat")
  125.         self.hates_dogs = hates_dogs
  126.     def hatesDogs(self):
  127.         return self.hates_dogs
  128.  
  129.  
  130.  
  131.  
  132.     # Main Loop #
  133. if __name__ == '__main__':
  134.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement