SV: Python Productivity over C++
Emile van Sebille
emile at fenx.com
Thu Jun 15 08:46:16 EDT 2000
I think I've got this working so that sub-classed
instances can be compared to subclassed references.
----- Original Message -----
From: Huaiyu Zhu <hzhu at knowledgetrack.com>
<snip>
>
> 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:
>
<snip>
> --
> Huaiyu Zhu
hzhu at users.sourceforge.net
> Matrix for Python Project
http://MatPy.sourceforge.net
> --
> http://www.python.org/mailman/listinfo/python-list
>
import types
def meths(items, meths=None):
if meths == None:
meths = {}
for obj in items:
for k, v in obj.__dict__.items():
if type(v) == types.FunctionType:
meths[k]=None
return meths
def bases(obj, items=None):
if items == None:
items = []
for b in getattr(obj, '__bases__', []):
bases(b, items)
items.append(b)
if type(obj) == types.ClassType:
items.append(obj)
return items
def verify_class (Reference, Instance):
r_meths = meths(bases(Reference))
i_meths = meths(bases(Instance))
for meth in r_meths.keys():
if not i_meths.has_key(meth):
print "Method %s not supported" % meth
Portions/ideas taken from both Kevin Jacobs
and from David Bolen.
Emile van Sebille
emile at fenx.com
-------------------
More information about the Python-list
mailing list