I’m trying to change the dictionary of a python class so that everytime a function object is added to the dictionary I add an instance of my class where I redefine __call__ instead.
I use a custom dictionary for the metaclass and in __setitem__ of the dictionary class I’m instantiating the class that I use to change the behaviour of the added functions and I add a not NoneType object to the dictionary. However, when I instantiate the class with the functions I’m wrapping and try to call the respective function I get a “NoneType not callable” error and the function object that I’ve wrapped in the class dictionary is = None. Any ideas why this might happen? Thanks!
I’m trying to change the dictionary of a python class so that everytime a
Share
Before I start – is the posted code your actual code? It doesn’t actually work for me under Python 3.2 – I’ve fixed it up a bit. Here’s what I’ve changed, to start:
is now
and in
OverloadedFunction.__call__:in
member_dict.__setitem__:I believe this is what you intended. I never got the
NoneType not callableerror – with these modifications (plusimport inspectat the top), your code works and prints the argument specification:Now, you want to implement the multimethod – the modifications I made preclude that, since your multimethods are overwritten on each call to
__setitem__, so you can work from there (perhaps usecollections.defaultdict(list)andappendtoclass_dict[key]instead).May I suggest something? I don’t think you need a metaclass – http://www.artima.com/weblogs/viewpost.jsp?thread=101605 provides a multimethod implementation for Python 2.x that’s very simple.