Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > trouble understanding inheritance...

Reply
Thread Tools

trouble understanding inheritance...

 
 
enigmadude
Guest
Posts: n/a
 
      08-17-2006
If what you're trying to do is have more control over the type of
object that is instantiated, then you could use a function that decides
what class to use based upon the arguments supplied to the function,
where it then instantiates an object from the chosen class, then
returns the object. The __init__ method is just for initialization when
an object is created, it's not a constructor and you could even leave
it out and still be able to create objects (although that's less useful
in most cases). Changing an object's type after it's already been
created is more advanced. It's useful, but I don't think that's what
you're trying to do. I believe you're just trying to allow the program
to have more control over what type of object to create. Try doing a
Google search for "creational design patterns", "factory function", and
"factory method". Here's a simple example of what I'm talking about:

def factory(chosen):
if chosen == 'a':
obj = typeA()
elif chosen == 'b':
obj = typeB()

return obj


KraftDiner wrote:
> This is not working the way I think it should....
> it would appear that fromfile and getName are calling the baseClass
> methods which are
> simple passes.... What have I done wrong?
>
> class baseClass:
> def __init__(self, type):
> if type == 'A':
> self = typeA()
> else:
> self = typeB()
> def fromfile(self):
> pass
> def getName(self):
> pass
>
> class typeA(baseClass):
> def __init__(self):
> self.name='A'
> print 'typeA init'
> def fromfile(self):
> print 'typeA fromfile'
> def getName(self):
> print self.name
>
> class typeB(baseClass):
> def __init__(self):
> self.name='B'
> print 'typeB init'
> def fromfile(self):
> print 'typeB fromfile'
> def getName(self):
> print self.name
>
> a = baseClass('A')
> a.fromfile()
> a.getName()
>
> b = baseClass('B')
> b.fromfile()
> b.getName()
>
> log:
> typeA init
> typeB init


 
Reply With Quote
 
 
 
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      08-27-2006
Steven D'Aprano a écrit :
(snip)
> class BaseClass():
> def foo(self):
> return "foo"
>
> class Foo(BaseClass):
> def foo(self):
> return self.__class__.foo() # call the parent class method


Err... May I suggest that you re-read the Fine Manual ?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
trouble understanding super() John Salerno Python 4 07-31-2006 05:43 PM
Trouble understanding StateServer!! Ric Pullen ASP .Net 1 07-23-2004 08:17 AM
Trouble Understanding O'Reilly Example slyraymond Python 5 04-26-2004 03:49 PM
Trouble understanding pointer to a const array. Rob C++ 1 03-07-2004 10:35 PM
trouble understanding None Jakle Python 9 11-13-2003 04:50 PM



Advertisments