Created
April 5, 2013 17:06
-
-
Save jhamrick/5320934 to your computer and use it in GitHub Desktop.
Demonstration of python classes and inheritance.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class 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 Dog(Pet): | |
def __init__(self, name, chases_cats): | |
Pet.__init__(self, name, "Dog") | |
self.chases_cats = chases_cats | |
def chasesCats(self): | |
return self.chases_cats | |
class Cat(Pet): | |
def __init__(self, name, hates_dogs): | |
Pet.__init__(self, name, "Cat") | |
self.hates_dogs = hates_dogs | |
def hatesDogs(self): | |
return self.hates_dogs |
Hi, I wanted to give you some tips on how to master Python.I love Python.what resources do you offer for me?
what solutions do you offer me newcomers.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you very much for the explanation. i have a question please. if i have something like this:
class Pet(object):
class Dog(Pet):
EOF
what if i want the class Dog to print out
name
is acolor
dog
inheriting from method str of class Pet. is it possible?if yes, how? thanks