Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python检查一个字符串是否在dir()中,以及它是否在将该字符串转换为方法/函数调用中_Python_Function_Methods - Fatal编程技术网

Python检查一个字符串是否在dir()中,以及它是否在将该字符串转换为方法/函数调用中

Python检查一个字符串是否在dir()中,以及它是否在将该字符串转换为方法/函数调用中,python,function,methods,Python,Function,Methods,在Python2.7中,如何将用户生成的字符串转换为方法或函数调用?您可以搜索dir(对象)以查看该方法/函数是否存在,然后调用该方法吗?最好尝试调用该方法,如果该方法不存在,它将抛出一个异常,您可以处理该异常 >>> try: obj.a_method() ... except AttributeError: print 'No method a_method in this object' ... No method a_method in this object >

在Python2.7中,如何将用户生成的字符串转换为方法或函数调用?您可以搜索dir(对象)以查看该方法/函数是否存在,然后调用该方法吗?

最好尝试调用该方法,如果该方法不存在,它将抛出一个异常,您可以处理该异常

>>> try: obj.a_method()
... except AttributeError: print 'No method a_method in this object'
... 
No method a_method in this object
>>> 

最好尝试调用该方法,如果该方法不存在,它将抛出一个异常,您可以处理该异常

>>> try: obj.a_method()
... except AttributeError: print 'No method a_method in this object'
... 
No method a_method in this object
>>> 

您可以尝试以下方法:

ui = input("Try something: ")
if ui in dir():
    func = eval(ui)
    func()
例如:

>>> def test():
    return "foo"

>>> if "test" in dir():
    func = eval("test")
    func()


'foo'

您可以尝试以下方法:

ui = input("Try something: ")
if ui in dir():
    func = eval(ui)
    func()
例如:

>>> def test():
    return "foo"

>>> if "test" in dir():
    func = eval("test")
    func()


'foo'

函数名只是属性,因此可以执行以下操作:

try:
    getattr(object, methodname)()
except AttributeError as e:
    print 'Method %s not found or not callable!'%methodname

函数名只是属性,因此可以执行以下操作:

try:
    getattr(object, methodname)()
except AttributeError as e:
    print 'Method %s not found or not callable!'%methodname

您可以使用
globals
它返回模块的
\uuuuuu dict\uuuuu

def command_1():                                                                
    print "You are in command 1"                                                

def command_2():                                                                
    print "You are in command 2"                                                

def default():                                                                  
    print "Can't find your command"                                             

func = raw_input("ENTER YOUR COMMAND: ")                                        
your_func = globals().get(func, None)                                           
if your_func is None:                                                                                                                                       
    your_func = default                                                         

your_func() 
输出: 或


您可以使用
globals
它返回模块的
\uuuuuu dict\uuuuu

def command_1():                                                                
    print "You are in command 1"                                                

def command_2():                                                                
    print "You are in command 2"                                                

def default():                                                                  
    print "Can't find your command"                                             

func = raw_input("ENTER YOUR COMMAND: ")                                        
your_func = globals().get(func, None)                                           
if your_func is None:                                                                                                                                       
    your_func = default                                                         

your_func() 
输出: 或


这里的想法是,既然您正在检查
dir
,那么只执行存在的对象,
eval
是安全的吗?@CorleyBrigman类似的想法,是的,或者肯定比
eval
检查任意字符串更安全。如果dir(object)调用返回列表或元组呢?我试着用你的方式,但运气不好。是因为我正试图用一个模块对象来实现这一点吗?具体地说,我正试图一步一步地遍历ast对象,只是为了与我的抽象语法树进行交互。@baallezx我认为您需要修改您的问题,提供更多关于您正在做什么以及您在哪里被卡住的信息(“运气不好”是否意味着错误(提供回溯)?意外输出(提供输入、预期和实际输出)?)这里的想法是,既然您正在检查
dir
,那么只会执行存在的对象,
eval
是安全的吗?@CorleyBrigman类似的东西,是的,或者肯定比
eval
任何任意字符串都更安全如果dir(对象)call返回一个列表或元组?我尝试使用您的方式,但运气不好。这是因为我试图对一个模块对象执行此操作吗?具体来说,我尝试一步一步地遍历ast对象,只是为了与我的抽象语法树交互。@baallezx我认为您需要修改您的问题,并提供更多有关您正在执行的操作以及在何处执行的信息确切地说,你被卡住了(“不走运”是否意味着错误(提供回溯)?意外输出(提供输入以及预期和实际输出)?)这解决了我的具体问题,谢谢。这解决了我的具体问题,谢谢。