Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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中使用importlib调用另一个模块中的类时出错_Python_Python 2.7 - Fatal编程技术网

在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)
的模块以及

我正在调用is
PlayAround\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