SV: Python Productivity over C++

Huaiyu Zhu hzhu at knowledgetrack.com
Wed Jun 14 20:04:03 EDT 2000


On 14 Jun 2000 19:11:08 -0400, David Bolen <db3l at fitlinxx.com> wrote:
>
>How about just writing a function to verify methods against a
>reference class.  The reference class could either be an actual class
>that you are mimicing the interface for, or a dummy class that is just
>defined with the appropriate methods.
>
>For a crude quick example, perhaps something like:
>
>    def verify_class (Reference, Instance):
>
>	import types
>
>	print "Checking %s for conformity with interface %s" % \
>	      (Instance.__name__,Reference.__name__)
>
>	for curmethod in Reference.__dict__.keys():
>	    if type(Reference.__dict__[curmethod]) == types.FunctionType:
>		if (not Instance.__dict__.has_key(curmethod) or
>		    type(Instance.__dict__[curmethod]) != types.FunctionType):
>		    print "Method %s not supported" % curmethod

Cool!  Things always look easier in python. :)

Now only if somebody could enhance that to check inherited methods too (the
"more general case").  That is, if a method is not found, go recursively
into the superclasses.  The following example is not correct at the moment:

------------------------------------------------------------------
class MyInterface:
    def method1():       pass
    def method2():       pass
    def method3():       pass
    def method4():       pass

class MyBase:
    "Actual implementations"
    def method1():       pass
    def method2():       pass

class MyDerived(MyBase):
    "Actual implementations"
    def method1():       pass
    def method3():       pass


if __name__ == "__main__":
    verify_class(MyInterface,MyBase)
    verify_class(MyInterface,MyDerived)
    print MyDerived().method2

------------------------------------------------------------------
gives 

Checking MyBase for conformity with interface MyInterface
Method method3 not supported
Method method4 not supported
Checking MyDerived for conformity with interface MyInterface
Method method2 not supported
Method method4 not supported
<method MyBase.method2 of MyDerived instance at 80cb550>



-- 
Huaiyu Zhu                       hzhu at users.sourceforge.net
Matrix for Python Project        http://MatPy.sourceforge.net 



More information about the Python-list mailing list