Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #-------------------------------------------------------------------------------
- # Name: ClassAndInheritance.py
- # Purpose: Study class inheritance
- #
- # Notes:
- #
- # References: http://www.jesshamrick.com/2011/05/18/an-introduction-to-classes-and-inheritance-in-python/
- #
- # Author: User
- #
- # Created: 04/06/2014
- # Copyright: (c) User 2014
- # Licence: <your licence>
- #-------------------------------------------------------------------------------
- # Imports #
- import locale
- from datetime import datetime
- # Configurations #
- locale.setlocale(locale.LC_ALL, '') # Set the locale for your system 'en_US.UTF-8'
- # Main #
- def main():
- # Variables
- # Global
- # Local
- # Main Code
- o_PetPolly = c_Pet("Polly", "Parrot") ## Calls main class to create this instance
- o_PetGinger = sc_Cat("Ginger", "True") ## Calls 'Cat" subclass with name and 'hates dogs' disposition
- o_PetClifford = sc_Dog("Clifford", "True") ## Calls 'Dog" subclass with name and 'chases cats' disposition
- print("Going to about a main class created instance")
- print(o_PetPolly)
- print("Typical way to call a method, 'o_PetPolly.getName()': %s") %(o_PetPolly.getName())
- print("Less typical way to call a method, 'Pet.getName(o_PetPolly)': %s") %(c_Pet.getName(o_PetPolly))
- print("Done.")
- print("")
- print("More about main class created instance") %()
- print("Obj name: %s") %(o_PetPolly.getName())
- print("Obj species: %s") %(o_PetPolly.getSpecies())
- print(o_PetPolly)
- print("")
- print("About this subclass created instance") %()
- print("Obj name: %s") %(o_PetGinger.getName())
- print("Obj species: %s") %(o_PetGinger.getSpecies())
- print("Obj hates dogs?: %s") %(o_PetGinger.hatesDogs())
- print(o_PetGinger)
- print("")
- print("And about this subclass created instance") %()
- print("Obj name: %s") %(o_PetClifford.getName())
- print("Obj species: %s") %(o_PetClifford.getSpecies())
- print("Obj chase cats?: %s") %(o_PetClifford.chasesCats())
- print(o_PetClifford)
- print("")
- print("Instance checks showing class and inheritance")
- mister_pet = c_Pet("Mister", "Dog")
- mister_dog = sc_Dog("Mister", True)
- print("Is mister_pet an instance of c_Pet?: %s") %(isinstance(mister_pet, c_Pet))
- print("Is mister_pet an instance of sc_Dog?: %s") %(isinstance(mister_pet, sc_Dog)) ## Not part of 'dog' as created via main class
- print("Is mister_dog an instance of c_Pet?: %s") %(isinstance(mister_dog, c_Pet)) ## Yes as came in via subclass 'dog'
- print("Is mister_dog an instance of sc_Dog?: %s") %(isinstance(mister_dog, sc_Dog))## Yes as came in via subclass 'dog'
- print("")
- print("Some pet instance facts.")
- o_Fido = sc_Dog("Fido", True) ## (name, chase cats?)
- o_Rover = sc_Dog("Rover", False) ## (name, chase cats?)
- o_Mittens = sc_Cat("Mittens", True) ## (name, hates dogs?)
- o_Fluffy = sc_Cat("Fluffy", False) ## (name, hates dogs?)
- print("I am: %s") %(o_Fido)
- print("I am: %s") %(o_Rover)
- print("I am: %s") %(o_Mittens)
- print("I am: %s") %(o_Fluffy)
- print("")
- print "%s chases cats: %s" %(o_Fido.getName(), o_Fido.chasesCats())
- print "%s chases cats: %s" %(o_Rover.getName(), o_Rover.chasesCats())
- print "%s hates dogs: %s" % (o_Mittens.getName(), o_Mittens.hatesDogs())
- print "%s hates dogs: %s" % (o_Fluffy.getName(), o_Fluffy.hatesDogs())
- # Tests
- # Output to file
- ## v_Path = 'C:/Users/User/Documents/out.txt'
- ## v_Mode = 'w'
- ## v_FileConn = f_OutputToFileOut(v_Path, v_Mode)
- ## sys.exit("Error message") ## Stop execution
- #------------------------------
- # Functions #
- # Normal Functions
- # Generator Functions
- # Lambda Functions
- # Classes #
- class c_Pet(object):
- def __init__(self, name, species):
- self.name = name
- self.species = species
- def getName(self):
- return self.name
- def getSpecies(self):
- return self.species
- def __str__(self):
- return "%s is a %s" % (self.name, self.species)
- class sc_Dog(c_Pet): ## Subclass of c_Pet
- def __init__(self, name, chases_cats):
- c_Pet.__init__(self, name, "Dog")
- self.chases_cats = chases_cats
- def chasesCats(self):
- return self.chases_cats
- class sc_Cat(c_Pet): ## Subclass of c_Pet
- def __init__(self, name, hates_dogs):
- c_Pet.__init__(self, name, "Cat")
- self.hates_dogs = hates_dogs
- def hatesDogs(self):
- return self.hates_dogs
- # Main Loop #
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement