擅长:python、mysql、java
<p>我知道这个问题很老了,但你可以这样做:</p>
<pre><code>argsdict = {'name': 'Joe', 'family': 'Brand'}
globals()['fullName'](**argsdict)
</code></pre>
<p><code>argsdict</code>是参数字典,<code>globals</code>使用字符串调用函数,<code>**</code>将字典扩展为参数列表。比<code>eval</code>干净得多。唯一的麻烦在于把绳子拆开。一个(非常混乱的)解决方案:</p>
<pre><code>example = 'fullName(name=\'Joe\',family=\'Brand\')'
# Split at left parenthesis
funcname, argsstr = example.split('(')
# Split the parameters
argsindex = argsstr.split(',')
# Create an empty dictionary
argsdict = dict()
# Remove the closing parenthesis
# Could probably be done better with re...
argsindex[-1] = argsindex[-1].replace(')', '')
for item in argsindex:
# Separate the parameter name and value
argname, argvalue = item.split('=')
# Add it to the dictionary
argsdict.update({argname: argvalue})
# Call our function
globals()[funcname](**argsdict)
</code></pre>