Reputation: 164
I am new to Python language. I have only 10 days experience on it. When I start learning there is no difficult, but it make me slow down when I reach "Object Oriented Concept", especially "Inherited".
Some of my background knowledge about "Inherited is the child class can get all of characteristic and behavior of parent class, in other words - data and methods of parent". Ok, I am going to show the concept that make me confuse with two programs. Both of them are getting the same result, so why people are making different.
First:
class Parent():
parentdata = 0
def __init__(self):
pass
def getParentData(self):
return Parent.parentdata
def setParentData(self, setdata):
Parent.parentdata = setdata
class Child(Parent):
childdata = 0
def __init__(self):
pass
def getChildData(self):
return Child.childdata
def setChildData(self, setdata):
Child.childdata = setdata
child = Child()
print "Default Child's Data is :" + str(child.getChildData())#getting 0
child.setChildData(3)
print "After Adding Child's Data is :"+ str(child.getChildData()) # getting 3
print "Default Parent's Data is:"+ str(child.getParentData())# getting 0
child.setParentData(1)
print "After Adding Parent's Data is :"+str(child.getParentData())# getting 1
Second:
class Parent():
parentdata = 0
def __init__(self):
pass
def getParentData(self):
return Parent.parentdata
def setParentData(self, setdata):
Parent.parentdata = setdata
class Child(Parent):
childdata = 0
def __init__(self):
#super(Child, self).__init__()
#super(Child, self).__init__(self, self)
Parent.__init__(self)
def getChildData(self):
return Child.childdata
def setChildData(self, setdata):
Child.childdata = setdata
child = Child()
print "Default Child's Data is :" + str(child.getChildData())#getting 0
child.setChildData(3)
print "After Adding Child's Data is :"+ str(child.getChildData()) # getting 3
print "Default Parent's Data is:"+ str(child.getParentData())# getting 0
child.setParentData(1)
print "After Adding Parent's Data is :"+str(child.getParentData())# getting 1
And please also guide me, how to use Super()
method for instance of Parent.__init__(self)
Somebody used, Super method in there and some are doing as my way.I am not clearly these two different.
In these two program - I am not using __init__
as constructor.
If I am going to use __init__
as to add data into the class's data(childdata,parentdata), how do I insert parameter in Parent.__init__(self)
and both of their def __init__(self):
method?
Upvotes: 0
Views: 216
Reputation: 12208
Your example seems to be behaving as expected. You have set up both classes with class level data -- that is, every Parent will be sharing the same value of parentData and every child will be sharing the same value of childData. Since your method names are not shared, your 'Child' instance will behave independently of 'Parent' except that calling setParentData on a Child will push the value into the state of all parents -- but NOT of all children.
The more common example would look like this:
class Parent(object):
shared_state = 0
def __init__(self):
self.private_state = 1
def get_shared(self):
return self.shared_state
def get_private(self)
return self.private_state
class Child(Parent):
shared_state = 2
def __init__(self):
Parent.__init__(self)
self.private_state = 3
#in this case calling the Parent constructor is not really needed but it's good practice
p = Parent()
p.get_shared()
> 0
p.get_private()
> 1
new_p = Parent()
new_p.get_shared()
> 0
c = Child()
c.get_shared()
> 2
c.get_private()
> 3
In this example all the parents share the variable 'shared_state' -- but each has it's own copy of private_state independent of the other Parents. Likewise each Child shares a copy of Child's shared_state -- which is different from Parent's shared state.
Essntially when the same name (for a class variable like shared_state) or a method (like get_private or get_shared) appears in a child class, the it overrides the original version. That's why calling get_shared() on a Child returns 2, which is Child.shared_state. In your original code you explicitly called Parent.parentData from Chold so you got the value from Parent.
The core idea behind imheritance is to make it easy to share common functionality and only write new classes or methods for what is specific to a particular need:
class Vehicle(object):
def __init__(self, name, passengers):
self.Passengers = passengers
self.Name = name
def go(self):
print "%n is going with %i passengers" % (self.Name, self.Passengers)
def get_name(self):
print self.Name
class Car(Vehicle):
def go(self):
print "%n is driving along with %i passengers" % (self.Name, self.Passengers)
class Boat(Vehicle):
def go(self):
print "%n is sailing along with %i passengers" % (self.Name, self.Passengers)
class Truck(Car):
def deliver(self):
print "%n is delivering cargo" % self.Name
Here the subclasses can all 'go' but they do it in different ways -- the nice thing is that code using them doesn't need to know what they are, it just calls go on them and they go in their own ways. The Truck 'goes' like a car and has a method for doing deliveries as well which the Boat and Car don't have. All of the classes share the 'get_name' method so it does not have to be rewritten -- it's there for free in all derived classes. So as you see you can share functions and data, replace them with different functions or data that have the same names (so outside code can get at them the same way) or add new functions and data.
You might want to check out Head First Python for a more thorough intro to these ideas. And of course there's a lot here on SO.
Upvotes: 1