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?
objectto get new style classes. Generally, python doesn't need to make use of getter and setter methods. Also, make use ofselfYou don't wantParent.parentdatayou wantself.parentdatanormally. That self attribute should probably be set in the__init__method.