Python use dictionary keys as function names
Question: I would like to be able to use dictionary keys as function names, but I'm not
sure if it's possible. As a quick example, instead of `class().dothis(dictkey,
otherstuff)`, I'd like to have an option for `class().dictkey(otherstuff)`.
Here's a not working code example to give an idea of what I was thinking of.

    
    
    class testclass:
        def __init__(self):
            self.dict = {'stuff':'value', 'stuff2':'value2'}
    
        #I know this part won't work, but it gives the general idea of what I'd like to do
        for key, value in self.dict.iteritems():
            def key():
                #do stuff
                return value
    
    >>> testclass().stuff()
    'value'
    

Obviously each key would need to be checked that it's not overriding anything
important, but other than that, I'd appreciate a bit of help if it's possible
to get working.

Basically, my script is to store other scripts in the headers of the Maya
scene file, so you may call a command and it'll execute the matching script.
It stores the scripts in text format in a dictionary, where I've done a
wrapper like thing so you can input args and kwargs without much trouble, and
because you can only enter and execute the scripts personally, there's
virtually no danger of anything being malicious unless you do it to yourself.

The list is pickled and base64 encoded as it all needs to be in string format
for the header, so each time the function is called it decodes the dictionary
so you can edit or read it, so ideally I'd need the functions built each time
it is called.

A couple of examples from the run function:

    
    
        Execute a simple line of code
        >>> SceneScript().add("MyScript", "print 5")
        >>> SceneScript().run("MyScript")
        5
    
        Execute a function with a return
        >>> SceneScript().add("MyScript", "def test(x): return x*5")
        >>> SceneScript().run("MyScript", "test(10)", "test('c')")
        [50, 'ccccc']
    
        Pass a variable to a function command
        >>> SceneScript().run("MyScript", 'test(a+b)', a=10, b=-50)
        [-200]
    
        Execute a function without a return
        >>> SceneScript().add("MyScript", "def test(x): print x*5")
        >>> SceneScript().run("MyScript", "test(10)", "test('c')")
        50
        ccccc
        [None, None]
    
        Pass a variable
        >>> SceneScript().add("MyScript", "print x")
        >>> SceneScript().run("MyScript", x=20)
        20
    

So as this question is asking, in terms of the above code, I'd like to have
something like `SceneScript().MyScript( "test(10)" )`, just to make it easier
to use.


Answer: The only "correct" way I can think of to do this looks like this:

    
    
    class SomeClass(object):
        def __init__(self, *args, **kwargs):
            funcs = {'funcname': 'returnvalue', ...}
            for func, ret_val in funcs.iteritems():
                setattr(self, func, self.make_function(ret_val))
    
        @staticmethod
        def make_function(return_value):
            def wrapped_function(*args, **kwargs):
                # do some stuff
                return return_value
            return wrapped_function
    

This should allow you do to:

    
    
    >>> foo = SomeClass()
    >>> foo.funcname()
    'returnvalue'
    

Of course the question of _why_ you'd want to do something like this remains,
as yet, unanswered :)

**_EDIT_** per updated question:

The problem lies in the fact that you cannot safely assign the method to the
function signature. I'm not sure how `SceneScript().add` works currently, but
that's essentially going to have to tie into this somehow or another.

