Understanding Class Inheritance in Python from learningPythonthehardway -
i have been learning python , understood inheritance means between classes , objects. here code want make sure got down right:
class animal(object): def __init__(self, name): self.name = name print self.name def howl(self, name): print "eeh %s" % name class dog(animal): def __init__(self, name): ##has-a __init__ function takes self , name parameters. self.name = name print "%s barks , happy" % self.name def sound(self, name): print "%s barks" % name rover = dog("rover") rover.sound("rover") rover.howl("rover") in order better understand way classes behaved "base class" animal, put prints on place , can see dog able call howl, function parent class, animal (is right?)
my other question when use rover = dog("rover"), how come it's using __init__ function call? purpose of __init__ function when set value variable (self.name)? because nobody calls rover.__init__("rover"), , can't print(rover = dog("rover")) , why didn't __init__ function of animal class print out?
just asking clarification here on class inheritance , function behaviors between related classes.
your post contains multiple questions here go!
1 dog able call howl function parent class, animal (is right?)
yes right. since declared dog class inheriting animal - class dog(animal) - dog class have attributes - including methods - declared in animal hence can call howl on dog.
2 when use rover = dog("rover"), how come it's using __init__ function call?
when creating object using classname(<parameters>) happening behind scene, among other things, python call __init__ method defined in class classname. more details see the __init__ documentation, indicates why class without __init__ method call parent's __init__ method. documentation answer sub-question what purpose of __init__ function ... ?. overall idea (almost) never call __init__ method directly.
now fun part!
3 why didn't __init__ function of animal class print out?
in code example __init__ method of animal , dog similar i.e. set name attribute , print something. difference being print. let's imagine rewrite classes this:
class animal(object): def __init__(self, name): self.name = name print "i animal named %s" % self.name class dog(animal): def __init__(self, name): super(dog, self).__init__(name) # that's important line print "i dog named %s, bark too!" % self.name rover = dog("rover") # output: # animal named rover # dog named rover, bark too! as commend explained import line when call super(dog, self).__init__(name) - see documentation - retrieve super class of dog , call __init__ method initialize current object self - hence setting name , calling print statement in animal class.
with end correct calls print!
note have several ways call super class's __init__:
animal.__init__(self, name) super(dog, self).__init__(name) super(self.__class__, self).__init__(name) # self.__class__ class of self or type. see this question more in depth comparison of various syntaxes.
Comments
Post a Comment