Slight metaclass confusion
David Mertz
mertz at gnosis.cx
Wed Sep 10 14:02:14 EDT 2003
|mro = getattr(obj, "mro", None)
|if mro:
| for i in mro:
| func = getattr(i, "function")
| if func:
| return func(*args, **kw)
|#Fall through
|return getattr(type(obj), "function")(obj, *args, **kw)
Pretty much, but it looks like you have a couple errors in your example.
I think this is better (also undertested):
def callmeth(obj, methname, *args, **kw):
meth = obj.__dict__.get(methname)
if meth is not None: # Might be in object's dict
return meth(*args, **kw)
if hasattr(obj, "mro"): # Might be in the ancestry
for klass in obj.mro():
meth = getattr(klass, methname)
if meth is not None:
return meth(*args, **kw)
# Or finally, look in class of obj
return getattr(type(obj), methname)(*args, **kw)
I think this does roughly the right thing if a non-callable "meth" is
encountered.
Yours, David...
--
_/_/_/ THIS MESSAGE WAS BROUGHT TO YOU BY: Postmodern Enterprises _/_/_/
_/_/ ~~~~~~~~~~~~~~~~~~~~[mertz at gnosis.cx]~~~~~~~~~~~~~~~~~~~~~ _/_/
_/_/ The opinions expressed here must be those of my employer... _/_/
_/_/_/_/_/_/_/_/_/_/ Surely you don't think that *I* believe them! _/_/
More information about the Python-list
mailing list