Abstract class

  • Thread starter Bruno Desthuilliers
  • Start date
R

Roy Smith

Maric Michaud said:
The only way to do this is to call in the __init__ at least one of the
methods raising NotImplementedError, probably creating a dummy one for
this purpose, and still it doesn't satisfy with the constraint that
*all* abstract methods must be implemented in concrete classes.

I wouldn't actually *call* the method (because of possible side-effects),
but you could certainly check that they exist and are callable:

class Base:
"""An abstract base class. You must define methods f(),
g(), and h() in any subclasses of this.
"""

def __init__(self):
try:
assert callable(self.f)
assert callable(self.g)
assert callable(self.h)
except:
raise NotImplementedError # or something more specific

Of course, this assumes that you write:

class Derived(Base):
def __init__(self):
Base.__init__(self)

There's no way (that I can think of :)) to *force* you to call
Base.__init__() from Derived.__init__().

This is all a bit silly, however. If you want type bondage, use a language
that gives you type bondage (C++ and Java being two obvious examples).
 
F

Fredrik Lundh

Maric said:
But this doesn't match what abstract classes are (in C++ for example)

you're confusing concepts with specific implementations of those
concepts here.

</F>
 
M

Maric Michaud

Le Monday 15 September 2008 16:42:49 Fredrik Lundh, vous avez écrit :
you're confusing concepts with specific implementations of those
concepts here.

Maybe, that's why I gave this example, but aren't they (conceptually)
classes :

- which couldn't be instantiated,
- which define at least an interface and possibly some behavior,
- for which all the undefined methods need to be implemented in concrete
classes (the pure virtual methodds in C++)

?

That's the exact implementation in C++ but I'm not aware of any other language
that implement this notion. Java simplified this a lot, abstracts are
interface (type), no behavior.
 

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

No members online now.

Forum statistics

Threads
473,800
Messages
2,569,657
Members
45,417
Latest member
BonitaNile
Top