Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.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 使用用户输入从另一个.py文件调用不同.py文件中定义的特定函数_Python - Fatal编程技术网

Python 使用用户输入从另一个.py文件调用不同.py文件中定义的特定函数

Python 使用用户输入从另一个.py文件调用不同.py文件中定义的特定函数,python,Python,我有一个包含一些函数的.py文件。所以它看起来像:- class Test_suites(): def fun1(): print("hello fun1 here") def fun2(): print("hello fun2 here") def fun3(): print("hello fun3 here") def fun4(): print("hello fun4 here") 现在我有了另一

我有一个包含一些函数的.py文件。所以它看起来像:-

class Test_suites():
    def fun1():
        print("hello fun1 here")
    def fun2():
        print("hello fun2 here")
    def fun3():
        print("hello fun3 here")
    def fun4():
      print("hello fun4 here")
现在我有了另一个文件,它接受用户的输入,并尝试从第一个python文件调用该特定函数。它看起来像:-

from test_suites import Test_Suites

ob=Test_Suites()
dictionary={1:'fun1',2:'fun2',3:'fun3',4:'fun4'}
user_input=eval(input("enter the test case number"))
string=dictionary[user_input]
ob.string()
但它抛出了一个错误:-ImportError:无法导入名称“Test_Suites”

请就如何解决这个问题给出一些见解。
谢谢

在您的代码中
字符串
是一个包含您要调用的函数名称的字符串
ob.string()
在对象
ob
上查找函数名和字符串。要从对象
obj
获取名为
name
的属性,请使用
getattr(obj,name)
,因此在您的情况下:

getattr(ob, string)()
正如Tagc在评论
eval
中指出的那样,您使用它的方式是一个坏主意。而是使用
user\u input=int(输入(“输入测试用例号”)
,将给定字符串解析为整数


如果您需要更灵活的功能,您可以从中使用
ast.literal\u eval
,它还可以解析列表、dicts。。。()

在您的代码中
string
是一个包含要调用的函数名称的字符串
ob.string()
在对象
ob
上查找函数名和字符串。要从对象
obj
获取名为
name
的属性,请使用
getattr(obj,name)
,因此在您的情况下:

getattr(ob, string)()
正如Tagc在评论
eval
中指出的那样,您使用它的方式是一个坏主意。而是使用
user\u input=int(输入(“输入测试用例号”)
,将给定字符串解析为整数


如果您需要更灵活的功能,您可以从中使用
ast.literal\u eval
,它还可以解析列表、dicts。。。()

如我在评论中所说,使用
getattr
按函数名访问函数

#test_suites.py
def fun1():
    print("hello fun1 here")
def fun2():
    print("hello fun2 here")
def fun3():
    print("hello fun3 here")
def fun4():
  print("hello fun4 here")


import test_suites

func1 = getattr(test_suites, 'func1')
# call func1()
#...

正如我在评论中所说,使用
getattr
通过函数名访问函数

#test_suites.py
def fun1():
    print("hello fun1 here")
def fun2():
    print("hello fun2 here")
def fun3():
    print("hello fun3 here")
def fun4():
  print("hello fun4 here")


import test_suites

func1 = getattr(test_suites, 'func1')
# call func1()
#...

从测试套件导入测试套件
应为
从测试套件导入测试套件
Suites
中的
s
在类定义中很小。按定义的方式导入。@Tagc不这样做。如果他尝试,他将遇到麻烦。要按函数名(从模块或类)运行函数,请使用
getattr(,“”)
是的,对不起,我想说的是代码有多危险。不要尝试OP删除它,但OP确实需要更改代码。
从测试套件导入测试套件
应该是
从测试套件导入测试套件
Suites
中的
s
在类定义中很小。按定义的方式导入。@Tagc不这样做。如果他尝试,他将遇到麻烦。要按函数名(从模块或类)运行函数,请使用
getattr(,“”)
是的,对不起,我想说的是代码有多危险。不要尝试OP删除了它,但OP确实需要更改代码。“而是使用
ast.literal\u eval
”在这种情况下,为什么不使用
int(input(…)
?@Tagc你是对的,在这种情况下它是没有意义的。但是我看不出使用
ast.literal\u eval
有什么坏处,但是在稍微不同的用例中
int(input())
可能不起作用,但是
literal\u eval
会起作用。对于这些情况,我想为其他用户指出正确的方向(
ast.literal\u eval
可能很难找到,如果你不知道它在哪里)。或者你看到在这里使用它有什么缺点吗?@syntonym的缺点是它不太清晰(
int(input(…)
更可读),如果用户没有输入整数,则会立即引发异常(最好尽早生成错误,以帮助跟踪问题)。“改用
ast.literal\u eval
”不过,为什么不在这种情况下只使用
int(input(…)
?@Tagc你是对的,在这种情况下这是毫无意义的。但是我看不出使用
ast.literal\u eval
,但在稍微不同的用例中
int(input())
可能不起作用,但
literal\u eval
会起作用。对于这些情况,我想为其他用户指出正确的方向(
ast.literal\u eval
可能很难找到,如果你不知道它在哪里)。或者你看到在这里使用它的任何缺点吗?@syntonym的缺点是它不太清晰(
int(input(…)
更具可读性),如果用户未输入整数,则会立即引发异常(最好尽早生成错误,以帮助跟踪问题)。