我想做以下事情:import mymodulem = mymodule.MyModule()m.dosth()# Does somethingm.more.domore()# Does moremymodule__init__.py文件如下所示:class MyModule(): def __init__(self): pass # Constructor def dosth(self): print("My module is doing something!") class more: def domore(self): print("My module is doing even more!")但是当我运行我的脚本时,会出现 TypeError: TypeError: domore() missing 1 required positional argument: 'self。如何在more不出错的情况下从类中调用方法?
2 回答
慕森卡
TA贡献1806条经验 获得超8个赞
此方法要么需要是静态的:
class MyModule():
def __init__(self):
pass # Constructor
def dosth(self):
print("My module is doing something!")
class more:
@staticmethod
def domore():
print("My module is doing even more!")
和
m = MyModule()
m.more.domore()
# or directly
MyModule.more.domore()
或者您需要more先创建一个实例:
m = MyModule.more()
m.domore()
添加回答
举报
0/150
提交
取消