Re: [Tutor] Python's OOP-Inheritance make me confuse.

2013-01-19 Thread Dave Angel

On 01/19/2013 02:08 AM, Moore John wrote:

Hi, 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 behaviour 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.
---Frist--
class Parent():

 parentdata = 0

 def __init__(self):

 pass

 SNIPL


Is there a reason that you doublespaced all the code?  It makes it hard 
to see much at a time on the screen.  Or is that a consequence of 
composing the mail as html (also a bad idea here, for several reasons).


First, by omitting the derivation from object, you've made Parent an old 
style class.  Change it to:

class Parent(object):

In version 3.*  Python has only new-style classes, and the change would 
be unnecessary.  But you're using version 2.x


After comparison, I see the only difference was the call to 
Parent.__init__().  That makes no difference, since the called function 
does nothing.  So of course the two files produce the same results.


The real question is what you expected either of them to do.  You have 
no instance data in either class, so the only thing you're sharing is 
the methods.  In other words, if you created ten instances of Chld, 
they'd all be identical


To use instance data, you need to use the self namespace.  So if the 
Parent class wants instance data called pdata, you'd do something like 
this inside the __init__() method.


 self.pdata = 42

Now, that data will be accessible in the child methods, by doing 
something like

  temp = self.pdata
  .do something with temp




---
And also guide me, how to use Super() method for instance of
Parent.__init__(self)
Somebody used, Super method ih 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.
Thanks



Parameters are done the same in __init__() method as in any other one. 
If you want to take 3 arguments, you might do something as simple as


def __init__(self, arg1, arg2, arg3):
self.data1 = arg1
self.data2 = arg2
self.data3 = arg3

Naturally, you'd pick better names.  Anyway, then you can create an 
instance by doing:


my_object = Parent(value1, value2, value3)

Now you have the beginnings of a useful class.  Each instance stores 
data which can be specified when the instance is created, and modified 
later.


Worry about inheritance after you see what a class is used for to begin 
with.  I think you should play with some realistic classes for a while, 
not just read about them.





--
DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Python's OOP-Inheritance make me confuse.

2013-01-18 Thread Moore John
Hi, 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 behaviour 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.
---Frist--
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 also guide me, how to use Super() method for instance of
Parent.__init__(self)
Somebody used, Super method ih 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.
Thanks
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor