1

I'm writing a class which I intend to use to create subroutines, constructor as following:

def __init__(self,menuText,RPC_params,RPC_call):
   #Treat the params
   #Call the given RPC_call with the treated params

The problem is that I want to call the function on the pattern "rpc.serve.(function name here)(params)", where rpc is a serverProxy object that I'm using to call XMLRPC functions, and serve.-function name- is the method I'm calling on the XMLRPC-server.

I've looked at Calling a function from a string with the function's name in Python, but seeing how my serverProxy object doesnt know which "remote attributes" it have, I cant use the getattr() function to retrieve the method.

I've seen a example by making a dictionary to call a given function, but is there no way to make the function truly dynamic by creating the function call as you would create a String? Like running a String as a function?

2
  • Why do you want this pattern? why not rpc.functionName(params)? Commented May 23, 2009 at 11:55
  • If you mean why I added "serve" ,its just for logical grouping of functions on the server, and nothing functional. Commented May 23, 2009 at 13:11

1 Answer 1

2

You can use getattr to get the function name from the server proxy, so calling the function like this will work:

getattr(rpc, function_name)(*params)
Sign up to request clarification or add additional context in comments.

2 Comments

You're right! Didnt know. But I still would like to know if its possible to run a given String as a method without using getattr() though. Even when I see the obvious pro's of using the getattr() method.
You could "run the string" via eval or exec, but that would be supremely [expletive deleted], with no advantage at all wrt getattr and more disadvantages than you can shake a stick at.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.