Why are so many built-in types inheritable?
Kent Johnson
kent at kentsjohnson.com
Fri Mar 24 23:25:07 EST 2006
Fabiano Sidler wrote:
> Hi folks!
>
> For debugging purposes I tried this:
>
> --- snip ---
> def foo(): pass
> function = type(foo)
>
> class PrintingFunction(function):
> def __init__(self, func):
> self.func = func
> def __call__(self, *args, **kwargs):
> print args, kwargs
> return function.__call__(self, args, kwargs)
>
> class DebugMeta(type):
> def __new__(self, name, bases, dict):
> for name in dict:
> if type(dict[name]) is function:
> dict[name] = PrintingFunction(dict[name])
>
> --- snap ---
>
> Now I tought I were able to let all maethod of classes with DebugMeta as
> metaclass print out their arguments. But I got the following sad error:
>
> TypeError: Error when calling the metaclass bases
> type 'function' is not an acceptable base type
>
> That's awful, isn't it?
> What could I do to get the above code working? (No, I disliked to re-
> implement <type 'function'> without this unpleasant behaviour in Python.)
You could do this with a simple decorator:
http://wiki.python.org/moin/PythonDecoratorLibrary#head-d4ce77c6d6e75aad25baf982f6fec0ff4b3653f4
or I think your class PrintingFunction would work as
class PrintingFunction(object):
def __init__(self, func):
self.func = func
def __call__(self, *args, **kwargs):
print args, kwargs
return self.func(*args, **kwargs)
Kent
More information about the Python-list
mailing list