Python Tutorial

Showing posts with label extending class. Show all posts
Showing posts with label extending class. Show all posts

Wednesday, August 15, 2012

python extending class



Sometimes we need to extend an existing class, lets do it some python way


#speak.py
class Speak(object):
    def __init__(self,tone):
        self.tone = tone
    def getTone(self):
        print self.tone



#anyName.py
from speak import Speak

class Bird(Speak):
    def __init__(self,birdTone):
        #construct super class
        Speak.__init__(self,birdTone)


class Cow(Speak):
    def __init__(self,cowTone):
        #another way to construct super class
        super(Cow, self).__init__(cowTone)
       

bird= Bird("Bird Talk")
bird.getTone()

cow= Cow("Cow Talk")
cow.getTone()



Output:
Bird Talk
Cow Talk