Python Scalability

dacut at kanga.org dacut at kanga.org
Tue May 25 11:48:28 EDT 1999


In article <etdwvxzo30y.fsf at m2-225-11.mit.edu>,
Alex <alex at somewhere.round.here> wrote:
> I don't know how much this will help you -- from your post, it seems
> like there are times when you should have added the method to a class,
> and you're getting an error because you failed to.

Exactly...

> But you can check
> whether a class instance has a certain method using something like
>
> import types
>
> def primma_donna_function (dubious_instance):
>     if (not hasttr (dubious_instance, 'method_name')) or \
>     	type (dubious_instance.method_name) != types.MethodType:
>     	raise 'What am I supposed to do with this incompetent \n' + \
>     	      'class instance?  Morons!  All they send me are \n' + \
>     	      'morons!'

Unfortunately, this doesn't work for the static checking that I want.
It just throws a different type of exception other than AttributeError
(which has all the information I need, really).  I want a way to make
sure that this doesn't happen before I even run the code.

> I'm hopelessly unqualified to talk about design issues, but the fact
> that you have to go through adding the same method to lots and lots of
> different functions suggests to me that you're not using inheritance
> effectively in your program's design.

Since Python doesn't have abstract methods ("pure virtual methods" in
C++ lingo), this doesn't help at all.  Again, you can do run-time
checking, ala:

class MyAbstractBase:
    def AbstractMethod(self):
        raise Exception, "Unimplemented method."

But this doesn't help static checking.  My question really is:  how can
I be sure that I never call MyAbstractBase.AbstractMethod() or
SomeClass.UndefinedMethod()?  A coverage tool could help, but isn't the
complete answer.

(N.B.:  Even C++ doesn't guarantee that an abstract method won't be
called.  It forbids you from creating a class with an abstract method,
but there are times (e.g., in the abstract class' ctor) when the
abstract method *can* be called.)


--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---




More information about the Python-list mailing list