我想做以下事情: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
提交
取消