I don't know if that will make sense, but ...
I am trying to dynamically assign methods to an object.
#translate this object.key(value) #into this object.method({key:value})
To be more specific in my example, I have an object (which I did not write), let's call it an engine that has several common methods, status, and several others. Some take a dictionary as an argument, and some take a list. To change the engine speed and see the result, I use:
motor.set({'move_at':10}) print motor.status('velocity')
The engine object then formats this request into a JSON-RPC string and sends it to the IO daemon. The author of the python object does not care what the arguments are, it just handles formatting and JSON sockets. The lines move_at and velocity are just two of the possible arguments.
Instead, I would like to do the following:
motor.move_at(10) print motor.velocity()
I would like to do this in a general way, because I have so many different arguments that I can pass. I do not want to do this:
# create a new function for every possible argument def move_at(self,x) return self.set({'move_at':x}) def velocity(self) return self.status('velocity')
I did a few searches on this subject, which suggested that the solution lies with lambdas and meta-programming, two subjects with which I could not tear myself away.
UPDATE:
Based on user470379 user code, I got the following ...
# This is what I have now.... class Motor(object): def set(self,a_dict): print "Setting a value", a_dict def status(self,a_list): print "requesting the status of", a_list return 10
output:
Setting a value {'move_at': 20} Setting a value {'move_at': 10} 10
Thanks to everyone who helped!