I am learning python and I understand what is meant by inheritance between classes and objects. So here is my code, I want to make sure that I got it right below:
class animal (object): def __init __ (self, name): self.name = name print Self.name def armor (self, name): print "eh% s"% name class dog (animal): def __init __ (self, name): ## - a __init__ function that takes the name and name parameter itself. Self. Name = name print "% s is bark and happy"% self.name def sound (self, name): print "% s bark"% name Rover = dog ("rover") rover. Sound ("rover") rover. Hole ("rover")
To better understand that my class behaves with "base class" animal
, I put print Given
everywhere and I can see that dog
is capable of calling howl
, a function from its original class, animal
(Is that correct?)
My second question was: How do I use it when I use the rover = dog ("rover")
Code> __init __ function type The answer is not it? What is the purpose of a __init __
function when it is actually set to a value for each variable ( self.name
)? Because no call rover .__ init __ ("rover") does not
, and you can not print print (rover = dog ("rover")) and why No
__ init __
print out of the animal
class?
The class is just asking for clarification on function behaviors between succession and related classes.
< 1 Yes, you are right. Since you have declared 2 When I use the When Now for the fun part! 3 Why did ' As is commendable, when we see Calls are ended, note that you have different syntax See more than in depth. dog
Animals
(Is that correct?) dog
class to be inherited by animal
- class dog
- the dog
All properties in the class - including methods - have been declared in Animals
, so you can call howl
on a dog. rover = ("rover")
, how it uses the __ init __
function call ClassName (& lt; Parameters & gt;)
What is happening behind the scenes, among other things, is that Python __ Init __
will define the method in class className
. To see more details, which also indicates why without a class __init __
method will call your parent's __init __
method. This documentation also answers your sub question What is the purpose of a __ init __
function ... ? The overall idea is that you (almost) never call directly to the __init __
method. Animal
print out of class __init __
method Animals are almost identical to
and dogs
i.e. set the name
attribute and print something. The only difference is what they print. So let's say that you want to write your classes in such a way:
class animal (object): def __init __ (self, name): self.name = name print "I have a name called% s The soul is the animal. "Self.name class dog (animal): def __init __ (self, name): super (dog, self) .__ init __ (name) # This important line is printed" I actually name a dog% S, I bark too! " % Self.name Rover = Dog ("Rover") # Output: #I am a creature named Rover # I am a dog named Rover, I also spit!
super (dog, self) .__ init __ (name)
- see - it will retrieve the super line Dog
and then we call the __init __
method to start the current object self
- hence the name
Setting and code in < Animal
category> Print Statement.
animal .__ init __ (self, name) super (dog) , Itself) .__ init __ (name) super (self .__ class__, itself) .__ init__ (name) # self__ class__ is self or its type of class
No comments:
Post a Comment