小编Mar*_*son的帖子

导入Python类的方法

我想知道是否可以将Python类的方法保存在与类定义不同的文件中,如下所示:

main_module.py:

class Instrument(Object):
    # Some import statement?
    def __init__(self):
        self.flag = True
    def direct_method(self,arg1):
        self.external_method(arg1, arg2)
Run Code Online (Sandbox Code Playgroud)

to_import_from.py:

def external_method(self, arg1, arg2):
    if self.flag:
        #doing something
#...many more methods
Run Code Online (Sandbox Code Playgroud)

就我而言,to_import_from.py是机器生成的,并包含许多方法.我宁愿不将它们复制粘贴到main_module.py中或逐个导入它们,但要将它们全部识别为Instrument类的方法,就像它们已在那里定义一样:

>>> instr = Instrument()
>>> instr.direct_method(arg1)
>>> instr.external_method(arg1, arg2)
Run Code Online (Sandbox Code Playgroud)

谢谢!

python methods import

10
推荐指数
5
解决办法
2万
查看次数

标签 统计

import ×1

methods ×1

python ×1