在python中使用importlib调用另一个模块中的类时出错
我试图从在python中使用importlib调用另一个模块中的类时出错,python,python-2.7,Python,Python 2.7,我试图从Sample2.py调用具有mod=importlib.import\u module(str)的模块以及 我正在调用isPlayAround\u Play.py当它只包含一个函数时,工作正常。如果我在函数中包含一个类 它工作不好。获取错误为TypeError:此构造函数不接受任何参数 sample2.py中的代码 PlayAround_Play.py中的代码 你们能告诉我一个如何使用importlib调用该类的解决方案吗 inst = getattr(mod, strs)() #c
Sample2.py
调用具有mod=importlib.import\u module(str)
的模块以及
我正在调用isPlayAround\u Play.py
当它只包含一个函数时,工作正常。如果我在函数中包含一个类
它工作不好。获取错误为TypeError:此构造函数不接受任何参数
sample2.py中的代码
PlayAround_Play.py中的代码
你们能告诉我一个如何使用importlib调用该类的解决方案吗
inst = getattr(mod, strs)() #creates an instance of the class
inst.PlayAround_Play(a, b, c) #call `PlayAround_Play` method on the instance.
输出:
60
要使代码正常工作,请在类中定义
\uuuuu init\uuuu
:
class PlayAround_Play():
def __init__(self, a, b, c):
d = a+b+c
print d
现在:
阅读:
\uuuu init\uuuu
,它不是以类命名的。。。你怎么知道importlib和getattr,却不知道python类的基础知识?。。。在阅读了其他问题之后,很明显,您对python一无所知,只从其他人那里得到了这段代码作为对前面问题的回答。在进一步提问之前,请先通过阅读了解该语言的基本知识。尝试使用不同的名称也会得到相同的错误..是。。我现在还不了解python语言学习,这似乎是对\uuuu init\uuuu
的错误使用,使用print语句而不是初始化。
60
class PlayAround_Play():
def __init__(self, a, b, c):
d = a+b+c
print d
getattr(mod, strs)(a, b, c)
#prints 60