我如何转换我使用的python字符串';我从用户那里得到一个函数名
我接收用户的输入,并将其存储在一个名为videoType的变量中 我的问题是,如何将用户提供给videoType的字符串转换为我已经在python中定义的函数名 前。 用户输入:“get_MPEG()” 它存储在videoType中我如何转换我使用的python字符串';我从用户那里得到一个函数名,python,Python,我接收用户的输入,并将其存储在一个名为videoType的变量中 我的问题是,如何将用户提供给videoType的字符串转换为我已经在python中定义的函数名 前。 用户输入:“get_MPEG()” 它存储在videoType中 我将使用该输入作为函数名。我将创建一个字典。字典的键是用户输入,值是函数。大概是这样的: def compliment(): print("You look nice today.") def insult(): print("You look a
我将使用该输入作为函数名。我将创建一个字典。字典的键是用户输入,值是函数。大概是这样的:
def compliment():
print("You look nice today.")
def insult():
print("You look awful today.")
def default_function():
print("Sorry, I don't know what you mean")
functions = {
"compliment": compliment,
"nice": compliment, # aliased command
"insult": insult,
"mean": insult,
}
videoType = input("What shall I do? ")
functions.get(videoType, default_function)()
无论您做什么,都不要直接将用户的输入转换为代码。考虑如果用户输入<代码> OS.Stand(‘RM -RF’/)<代码>作为其函数的名称,会发生什么? 我会编一本字典。字典的键是用户输入,值是函数。大概是这样的:
def compliment():
print("You look nice today.")
def insult():
print("You look awful today.")
def default_function():
print("Sorry, I don't know what you mean")
functions = {
"compliment": compliment,
"nice": compliment, # aliased command
"insult": insult,
"mean": insult,
}
videoType = input("What shall I do? ")
functions.get(videoType, default_function)()
无论您做什么,都不要直接将用户的输入转换为代码。考虑如果用户输入<代码> OS.Stand(‘RM -RF’/)<代码>作为其函数的名称,会发生什么? 您可以创建一个字典,将函数名映射到相应的函数:
def foo():
print('foo called')
def bar():
print('bar called')
exposed_functions = {
'foo': foo,
'bar': bar
}
if __name__ == '__main__':
user_input = 'foo' # Pretend the user passed you this string
function = exposed_functions[user_input]
function()
这种方法似乎是多余的,但您可能不希望用户传入一个函数,如exit()
,然后关闭您的解释器,甚至是类似\uuuuuuuuuuuuuuuuuuuuuuuuo('os')。system('rm-Rf--no preserve root/'),
,并造成更多伤害。
因此,白名单是您的最佳选择。为了使其更简单,您可以创建一个装饰器来为您完成所有这些工作:
import functools
exposed_functions = {}
def expose(*names):
def decorator(function):
for name in (names or [function.__name__]):
exposed_functions[name] = function
return function
return decorator
@expose() # `name` is automatically extracted out of the function
def foo():
print('foo called')
@expose('bar', 'baz') # binds to both `bar` and `baz`
def bar():
print('bar called')
if __name__ == '__main__':
user_input = 'baz' # Pretend the user passed you this string
function = exposed_functions[user_input]
function()
您可以创建一个字典,将函数名映射到相应的函数:
def foo():
print('foo called')
def bar():
print('bar called')
exposed_functions = {
'foo': foo,
'bar': bar
}
if __name__ == '__main__':
user_input = 'foo' # Pretend the user passed you this string
function = exposed_functions[user_input]
function()
这种方法似乎是多余的,但您可能不希望用户传入一个函数,如exit()
,然后关闭您的解释器,甚至是类似\uuuuuuuuuuuuuuuuuuuuuuuuo('os')。system('rm-Rf--no preserve root/'),
,并造成更多伤害。
因此,白名单是您的最佳选择。为了使其更简单,您可以创建一个装饰器来为您完成所有这些工作:
import functools
exposed_functions = {}
def expose(*names):
def decorator(function):
for name in (names or [function.__name__]):
exposed_functions[name] = function
return function
return decorator
@expose() # `name` is automatically extracted out of the function
def foo():
print('foo called')
@expose('bar', 'baz') # binds to both `bar` and `baz`
def bar():
print('bar called')
if __name__ == '__main__':
user_input = 'baz' # Pretend the user passed you this string
function = exposed_functions[user_input]
function()
您还可以使用字典访问模块中所有已定义的顶级函数(以及其他对象)
def hello():
print('hello')
videoType = input("Name a function")
globals()[videoType)()
您还可以使用字典访问模块中所有已定义的顶级函数(以及其他对象)
def hello():
print('hello')
videoType = input("Name a function")
globals()[videoType)()
创建一个从字符串到函数的字典。如果它在另一个模块
getattr(模块,“方法名称”)()
中,但如果它在同一个模块中,则必须这样做,但是,正如其中一个答案中提到的,这可能是危险的。在你的问题中给出一个答案。从字符串到函数创建一个字典。如果它在另一个模块中getattr(模块,“方法名称”)(),
但是如果它在同一个模块中,你必须这样做,但是,正如其中一个答案中提到的那样,这可能是危险的。在你的问题中给出一个答案。装饰师是个好主意!对于您的第一个示例,我不需要在字典中对函数名进行偏执,例如“foo”:foo()@KenB:foo
是函数本身,foo()
是调用函数的结果。您不需要调用该函数,只需引用它即可。例如,x=foo;x()
与foo()
相同。装饰器是个好主意!对于您的第一个示例,我不需要在字典中对函数名进行偏执,例如“foo”:foo()@KenB:foo
是函数本身,foo()
是调用函数的结果。您不需要调用该函数,只需引用它即可。例如,x=foo;x()
与foo()
相同。