Abstract Classes

T

Tim Cook

I am implementing a set of specifications that were designed to be OO
language neutral.

Several classes are specified as being abstract; so therefore there
should be no instances of them, correct?

However in the classes that are subclasses what is the correct way in
Python to implement them?
I am using the zope.interface module as well.

For example:

class IA(Interface):
m = Int()
p = TextLine()

class IB(Interface):

x = Bool()
s = Text()



class A(object):
""" This is an abstract class with attributes m is an int and p is a
string"""

implements(IA)
pass

class B(A):
implements(IB)

def __init__(self,m,p,x,s):
m=m
p=p
x=x
s=s


or should it be like:

class A(object):
""" This is an abstract class with attributes m is an int and p is a
string"""

implements(IA)

def __init__(self,m,p):
m=m
p=p


class B(A):
implements(IB)

def __init__(self,m,p,x,s):
A.__init__(m,p)
x=x
s=s


or maybe even:

class B(A):
implements(IB)

def __init__(self,m,p,x,s):
super(A.__init__(m,p))
x=x
s=s

Thanks for any pointers.

Tim


--
Timothy Cook, MSc
Health Informatics Research & Development Services
LinkedIn Profile:http://www.linkedin.com/in/timothywaynecook
Skype ID == timothy.cook
**************************************************************
*You may get my Public GPG key from popular keyservers or *
*from this link http://timothywayne.cook.googlepages.com/home*
**************************************************************
 
R

Rhodri James

I am implementing a set of specifications that were designed to be OO
language neutral.

Several classes are specified as being abstract; so therefore there
should be no instances of them, correct?

However in the classes that are subclasses what is the correct way in
Python to implement them?

That very much depends on the classes, and how much the abstract classes
do. With the very simple examples you give which have no methods even,
I'd be strongly tempted not to implement the classes at all. But let's
assume that's not a good idea (and there are plenty of reasons for it
not to be a good idea, to be fair).

Caution: I am not in the slightest bit familiar with the zope.interface
module, which does make your IA and IB classes look exceptionally
pointless. Again, it's fairly safe to presume that I'm wrong there :)
I am using the zope.interface module as well.

For example:

class IA(Interface):
m = Int()
p = TextLine()

class IB(Interface):

x = Bool()
s = Text()



class A(object):
""" This is an abstract class with attributes m is an int and p is a
string"""

implements(IA)
pass

class B(A):
implements(IB)

def __init__(self,m,p,x,s):
m=m
p=p
x=x
s=s

Presumably you mean
self.m = m
self.p = p
and so on?

This doesn't seem to be a consistent approach. Class B enforces
initialisation of all its attributes, including the inherited ones,
but class A doesn't.
or should it be like:

class A(object):
""" This is an abstract class with attributes m is an int and p is a
string"""

implements(IA)

def __init__(self,m,p):
m=m
p=p


class B(A):
implements(IB)

def __init__(self,m,p,x,s):
A.__init__(m,p)
x=x
s=s

Modulo the "self." as before, this is more consistent.
or maybe even:

class B(A):
implements(IB)

def __init__(self,m,p,x,s):
super(A.__init__(m,p))
x=x
s=s

I have a bit of an allergy to super() because it works in slightly
unexpected ways. Here in particular it doesn't gain you anything
and may cost you instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top