[Tutor] Python's OOP-Inheritance make me confuse.
Dave Angel
d at davea.name
Sat Jan 19 09:37:48 CET 2013
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
>
> <SNIP>L
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
More information about the Tutor
mailing list