Questo è per l'uso in un'API JSON. Io non voglio avere:Python: come si chiama un metodo quando si ha solo il nome stringa del metodo?
if method_str == 'method_1':
method_1()
if method_str == 'method_2':
method_2()
Per ovvie ragioni questo non è ottimale. Come potrei usare le stringhe della mappa su metodi come questo in un modo riusabile (notare anche che ho bisogno di passare argomenti alle funzioni chiamate).
Ecco un esempio:
INCOMING JSON:
{
'method': 'say_something',
'args': [
135487,
'a_465cc1'
]
'kwargs': {
'message': 'Hello World',
'volume': 'Loud'
}
}
# JSON would be turned into Python with Python's built in json module.
chiamata risultante:
# Either this
say_something(135487, 'a_465cc1', message='Hello World', volume='Loud')
# Or this (this is more preferable of course)
say_something(*args, **kwargs)
dupe molte volte su – SilentGhost
dupe: http://stackoverflow.com/questions/680941/python-dynamic-function-names – SilentGhost
Se non è per l'utilizzo in un'API, rispetto al perché menziona JSON in entrata e con il metodo nome specificato nell'oggetto JSON? Pensi che sia così che posso costruire una stringa JSON nel mio codice Python e poi passarla per chiamare i metodi? – orokusaki