Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > inheritance?

Reply
Thread Tools

inheritance?

 
 
KraftDiner
Guest
Posts: n/a
 
      08-16-2006
I have two classes:

class implicitClass:
def __init__(self):
def isIVR(self): #This is a class private method.
def fromfile(self, fileObj, byteOrder):
def getVR(self):
def getGroup(self):
def getElement(self):
def getSize(self):
def getData(self):

class explicitClass:
def __init__(self):
def fromfile(self, fileObj):
def getVR(self):
def getGroup(self):
def getElement(self):
def getSize(self):
def getData(self):


As you can see the interface is almost identical.

How can I define a base class that will abstract
the type such that I don't know if its really and inplicit
or explicit object?

 
Reply With Quote
 
 
 
 
John Henry
Guest
Posts: n/a
 
      08-16-2006
Is this what you're looking for?

class baseClass:
def __init__(self):
def fromfile(self, fileObj, byteOrder=None):
def getVR(self):
def getGroup(self):
def getElement(self):
def getSize(self):
def getData(self):

class implicitClass(baseClass):
def __init__(self):
baseClass.__init__(self)
def isIVR(self): #This is a class private method.

class explicitClass(baseClass):
def __init__(self):
baseClass.__init__(self)

KraftDiner wrote:
> I have two classes:
>
> class implicitClass:
> def __init__(self):
> def isIVR(self): #This is a class private method.
> def fromfile(self, fileObj, byteOrder):
> def getVR(self):
> def getGroup(self):
> def getElement(self):
> def getSize(self):
> def getData(self):
>
> class explicitClass:
> def __init__(self):
> def fromfile(self, fileObj):
> def getVR(self):
> def getGroup(self):
> def getElement(self):
> def getSize(self):
> def getData(self):
>
>
> As you can see the interface is almost identical.
>
> How can I define a base class that will abstract
> the type such that I don't know if its really and inplicit
> or explicit object?


 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      08-16-2006
On Tue, 15 Aug 2006 19:35:11 -0700, KraftDiner wrote:

> I have two classes:
>
> class implicitClass:
> def __init__(self):
> def isIVR(self): #This is a class private method.


The convention is to flag classes as "private" with a leading underscore:

def _isIVR(self):

or double underscores for "really private, no, honestly":

def __isIVR(self):

but google on "python name mangling" before using that.

[snip]

> As you can see the interface is almost identical.
>
> How can I define a base class that will abstract
> the type such that I don't know if its really and inplicit
> or explicit object?


Something like this?


class baseClass:
def __init__(self):
raise NotImplementedError("Don't instantiate the base class!")
def fromfile(self):
def getElement(self):
# etc.


class implicitClass(baseClass):
def __init__(self):
# code
def _isIVR(self):
# code
def fromfile(self, fileObj, byteOrder):
# code
# etc.

Now, you can define instance = implicitClass() or explicitClass(). When
you come to use instance, you don't need to know whether it is one or the
other. If you need to type-test, call "isinstance(instance, baseClass)".

The only minor issue is that the fromfile method has a different
interface. If you really want to do duck typing, they need to have the
same interface. That might be as simple as:

class explicitClass(baseClass):
def fromfile(self, fileObj, byteOrder=None):
# byteOrder is ignored; it is included only for
# compatibility with implicitClass


Is that what you're asking for?



--
Steven D'Aprano

 
Reply With Quote
 
KraftDiner
Guest
Posts: n/a
 
      08-16-2006

Steven D'Aprano wrote:
> On Tue, 15 Aug 2006 19:35:11 -0700, KraftDiner wrote:
>
> > I have two classes:
> >
> > class implicitClass:
> > def __init__(self):
> > def isIVR(self): #This is a class private method.

>
> The convention is to flag classes as "private" with a leading underscore:
>
> def _isIVR(self):
>
> or double underscores for "really private, no, honestly":
>
> def __isIVR(self):
>
> but google on "python name mangling" before using that.
>
> [snip]
>
> > As you can see the interface is almost identical.
> >
> > How can I define a base class that will abstract
> > the type such that I don't know if its really and inplicit
> > or explicit object?

>
> Something like this?
>
>
> class baseClass:
> def __init__(self):
> raise NotImplementedError("Don't instantiate the base class!")
> def fromfile(self):
> def getElement(self):
> # etc.
>
>
> class implicitClass(baseClass):
> def __init__(self):
> # code
> def _isIVR(self):
> # code
> def fromfile(self, fileObj, byteOrder):
> # code
> # etc.
>
> Now, you can define instance = implicitClass() or explicitClass(). When
> you come to use instance, you don't need to know whether it is one or the
> other. If you need to type-test, call "isinstance(instance, baseClass)".
>
> The only minor issue is that the fromfile method has a different
> interface. If you really want to do duck typing, they need to have the
> same interface. That might be as simple as:
>
> class explicitClass(baseClass):
> def fromfile(self, fileObj, byteOrder=None):
> # byteOrder is ignored; it is included only for
> # compatibility with implicitClass
>
>
> Is that what you're asking for?
>

Yes I believe so but in fromfile I want to call the appropriate
method depending on the in a parameter in fromfile...
like:
class baseClass:
def fromfile(self, fileObj, byteOrder=None, explicit=False):
if explicit:
call fromfile of explicit class
else:
call fromfile of implicit class

How is that done?


>
>
> --
> Steven D'Aprano


 
Reply With Quote
 
infidel
Guest
Posts: n/a
 
      08-16-2006
> Yes I believe so but in fromfile I want to call the appropriate
> method depending on the in a parameter in fromfile...
> like:
> class baseClass:
> def fromfile(self, fileObj, byteOrder=None, explicit=False):
> if explicit:
> call fromfile of explicit class
> else:
> call fromfile of implicit class


class baseClass:
def fromfile(self, fileObj, **kwargs):
raise NotImplementedError()

class implicitClass:
def fromfile(self, fileObj, **kwargs):
byteOrder = kwargs.get('byteOrder', None)
# do something

class explicitClass:
def fromfile(self, fileObj, **kwargs):
# do something

 
Reply With Quote
 
John Henry
Guest
Posts: n/a
 
      08-16-2006
As others pointed out already, this kind of "if then else"
determination of type is best avoided.

If it looks like a duck, quakes like a duck, must be a duck.

KraftDiner wrote:
> Steven D'Aprano wrote:
> > On Tue, 15 Aug 2006 19:35:11 -0700, KraftDiner wrote:
> >
> > > I have two classes:
> > >
> > > class implicitClass:
> > > def __init__(self):
> > > def isIVR(self): #This is a class private method.

> >
> > The convention is to flag classes as "private" with a leading underscore:
> >
> > def _isIVR(self):
> >
> > or double underscores for "really private, no, honestly":
> >
> > def __isIVR(self):
> >
> > but google on "python name mangling" before using that.
> >
> > [snip]
> >
> > > As you can see the interface is almost identical.
> > >
> > > How can I define a base class that will abstract
> > > the type such that I don't know if its really and inplicit
> > > or explicit object?

> >
> > Something like this?
> >
> >
> > class baseClass:
> > def __init__(self):
> > raise NotImplementedError("Don't instantiate the base class!")
> > def fromfile(self):
> > def getElement(self):
> > # etc.
> >
> >
> > class implicitClass(baseClass):
> > def __init__(self):
> > # code
> > def _isIVR(self):
> > # code
> > def fromfile(self, fileObj, byteOrder):
> > # code
> > # etc.
> >
> > Now, you can define instance = implicitClass() or explicitClass(). When
> > you come to use instance, you don't need to know whether it is one or the
> > other. If you need to type-test, call "isinstance(instance, baseClass)".
> >
> > The only minor issue is that the fromfile method has a different
> > interface. If you really want to do duck typing, they need to have the
> > same interface. That might be as simple as:
> >
> > class explicitClass(baseClass):
> > def fromfile(self, fileObj, byteOrder=None):
> > # byteOrder is ignored; it is included only for
> > # compatibility with implicitClass
> >
> >
> > Is that what you're asking for?
> >

> Yes I believe so but in fromfile I want to call the appropriate
> method depending on the in a parameter in fromfile...
> like:
> class baseClass:
> def fromfile(self, fileObj, byteOrder=None, explicit=False):
> if explicit:
> call fromfile of explicit class
> else:
> call fromfile of implicit class
>
> How is that done?
>
>
> >
> >
> > --
> > Steven D'Aprano


 
Reply With Quote
 
KraftDiner
Guest
Posts: n/a
 
      08-16-2006

Steven D'Aprano wrote:
> On Tue, 15 Aug 2006 19:35:11 -0700, KraftDiner wrote:
>
> > I have two classes:
> >
> > class implicitClass:
> > def __init__(self):
> > def isIVR(self): #This is a class private method.

>
> The convention is to flag classes as "private" with a leading underscore:
>
> def _isIVR(self):
>
> or double underscores for "really private, no, honestly":
>
> def __isIVR(self):
>
> but google on "python name mangling" before using that.
>
> [snip]
>
> > As you can see the interface is almost identical.
> >
> > How can I define a base class that will abstract
> > the type such that I don't know if its really and inplicit
> > or explicit object?

>
> Something like this?
>
>
> class baseClass:
> def __init__(self):
> raise NotImplementedError("Don't instantiate the base class!")
> def fromfile(self):
> def getElement(self):
> # etc.
>
>
> class implicitClass(baseClass):
> def __init__(self):
> # code
> def _isIVR(self):
> # code
> def fromfile(self, fileObj, byteOrder):
> # code
> # etc.
>
> Now, you can define instance = implicitClass() or explicitClass(). When
> you come to use instance, you don't need to know whether it is one or the
> other. If you need to type-test, call "isinstance(instance, baseClass)".
>
> The only minor issue is that the fromfile method has a different
> interface. If you really want to do duck typing, they need to have the
> same interface. That might be as simple as:
>
> class explicitClass(baseClass):
> def fromfile(self, fileObj, byteOrder=None):
> # byteOrder is ignored; it is included only for
> # compatibility with implicitClass
>
>
> Is that what you're asking for?
>
>

Here I tried this example and maybe this will explain the difficulties
I'm having.
1) at the time the baseClass is constructed shouldn't the constructor
of the appropriate
type be called.
2) getName is doing nothing...

class baseClass:
def __init__(self):
pass
def fromfile(self, str):
if (str == 'A'):
a = typeA()
else:
a = typeB()
def getName(self):
pass

class typeA(baseClass):
def __init__(self):
self.name='A'
print 'typeA init'
def fromfile(self, str=None):
print 'typeA fromfile'
def getName(self):
print self.name

class typeB(baseClass):
def __init__(self):
self.name='B'
print 'typeB init'
def fromfile(self, str=None):
print 'typeB fromfile'
def getName(self):
print self.name

bc = baseClass()
bc.fromfile('A')
bc.getName()
bc.fromfile('B')
bc.getName()
bc.getName()

log:
typeA init
typeB init






>
> --
> Steven D'Aprano


 
Reply With Quote
 
Inyeol Lee
Guest
Posts: n/a
 
      08-16-2006
On Wed, Aug 16, 2006 at 11:07:08AM -0700, KraftDiner wrote:
[...]
> Here I tried this example and maybe this will explain the difficulties
> I'm having.
> 1) at the time the baseClass is constructed shouldn't the constructor
> of the appropriate
> type be called.
> 2) getName is doing nothing...
>
> class baseClass:
> def __init__(self):
> pass
> def fromfile(self, str):
> if (str == 'A'):
> a = typeA()
> else:
> a = typeB()
> def getName(self):
> pass
>
> class typeA(baseClass):
> def __init__(self):
> self.name='A'
> print 'typeA init'
> def fromfile(self, str=None):
> print 'typeA fromfile'
> def getName(self):
> print self.name
>
> class typeB(baseClass):
> def __init__(self):
> self.name='B'
> print 'typeB init'
> def fromfile(self, str=None):
> print 'typeB fromfile'
> def getName(self):
> print self.name
>
> bc = baseClass()
> bc.fromfile('A')
> bc.getName()
> bc.fromfile('B')
> bc.getName()
> bc.getName()
>
> log:
> typeA init
> typeB init
>



I didn't follow up this thread from the begining, but I think this is
what you want;

Python 2.4.3 (#1, Mar 31 2006, 09:09:53)
[GCC 2.95.3 20010315 (release)] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>> class baseClass:

.... @staticmethod
.... def fromfile(string):
.... if string == "A":
.... return typeA()
.... else:
.... return typeB()
.... def getName(self):
.... print self.name
....
>>> class typeA(baseClass):

.... def __init__(self):
.... self.name = "A"
.... print "typeA init"
....
>>> class typeB(baseClass):

.... def __init__(self):
.... self.name = "B"
.... print "typeB init"
....
>>> a = baseClass.fromfile("A")

typeA init
>>> a.getName()

A
>>> b = baseClass.fromfile("B")

typeB init
>>> b.getName()
>>> B
>>>


--
Inyeol Lee
 
Reply With Quote
 
John Henry
Guest
Posts: n/a
 
      08-16-2006


> Here I tried this example and maybe this will explain the difficulties
> I'm having.
> 1) at the time the baseClass is constructed shouldn't the constructor
> of the appropriate
> type be called.


Not automatically.

> 2) getName is doing nothing...
>
> class baseClass:
> def __init__(self):
> pass
> def fromfile(self, str):
> if (str == 'A'):
> a = typeA()
> else:
> a = typeB()
> def getName(self):
> pass
>
> class typeA(baseClass):
> def __init__(self):
> self.name='A'
> print 'typeA init'
> def fromfile(self, str=None):
> print 'typeA fromfile'
> def getName(self):
> print self.name
>
> class typeB(baseClass):
> def __init__(self):
> self.name='B'
> print 'typeB init'
> def fromfile(self, str=None):
> print 'typeB fromfile'
> def getName(self):
> print self.name
>
> bc = baseClass()
> bc.fromfile('A')
> bc.getName()
> bc.fromfile('B')
> bc.getName()
> bc.getName()
>
> log:
> typeA init
> typeB init
>


> >
> > --
> > Steven D'Aprano


Maybe this would help:

class baseClass:
def __init__(self, name):
self.name=name
print 'type'+self.name+' init'
def fromfile(self, str):
if (str == 'A'):
a = typeA()
else:
a = typeB()
def getName(self):
print self.name

class typeA(baseClass):
def __init__(self):
baseClass.__init__(self, "A")
def fromfile(self, str=None):
print 'type'+self.name+' fromfile'

class typeB(baseClass):
def __init__(self):
baseClass.__init__(self, "B")
def fromfile(self, str=None):
print 'type'+self.name+' fromfile'

bcA = typeA()
bcA.fromfile()
bcA.getName()

bcB = typeB()
bcB.fromfile()
bc.getName()

I think you're looking at objects in an inverted way.

typeA is a kind of baseClass, and so is typeB.

not:

baseClass consists of 2 subclasses A and B.

 
Reply With Quote
 
John Henry
Guest
Posts: n/a
 
      08-16-2006
Oops! Forgot to remove fromfile from baseClass. Wouldn't matter
though.

John Henry wrote:
> > Here I tried this example and maybe this will explain the difficulties
> > I'm having.
> > 1) at the time the baseClass is constructed shouldn't the constructor
> > of the appropriate
> > type be called.

>
> Not automatically.
>
> > 2) getName is doing nothing...
> >
> > class baseClass:
> > def __init__(self):
> > pass
> > def fromfile(self, str):
> > if (str == 'A'):
> > a = typeA()
> > else:
> > a = typeB()
> > def getName(self):
> > pass
> >
> > class typeA(baseClass):
> > def __init__(self):
> > self.name='A'
> > print 'typeA init'
> > def fromfile(self, str=None):
> > print 'typeA fromfile'
> > def getName(self):
> > print self.name
> >
> > class typeB(baseClass):
> > def __init__(self):
> > self.name='B'
> > print 'typeB init'
> > def fromfile(self, str=None):
> > print 'typeB fromfile'
> > def getName(self):
> > print self.name
> >
> > bc = baseClass()
> > bc.fromfile('A')
> > bc.getName()
> > bc.fromfile('B')
> > bc.getName()
> > bc.getName()
> >
> > log:
> > typeA init
> > typeB init
> >

>
> > >
> > > --
> > > Steven D'Aprano

>
> Maybe this would help:
>
> class baseClass:
> def __init__(self, name):
> self.name=name
> print 'type'+self.name+' init'
> def fromfile(self, str):
> if (str == 'A'):
> a = typeA()
> else:
> a = typeB()
> def getName(self):
> print self.name
>
> class typeA(baseClass):
> def __init__(self):
> baseClass.__init__(self, "A")
> def fromfile(self, str=None):
> print 'type'+self.name+' fromfile'
>
> class typeB(baseClass):
> def __init__(self):
> baseClass.__init__(self, "B")
> def fromfile(self, str=None):
> print 'type'+self.name+' fromfile'
>
> bcA = typeA()
> bcA.fromfile()
> bcA.getName()
>
> bcB = typeB()
> bcB.fromfile()
> bc.getName()
>
> I think you're looking at objects in an inverted way.
>
> typeA is a kind of baseClass, and so is typeB.
>
> not:
>
> baseClass consists of 2 subclasses A and B.


 
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




Advertisments