我已经用python2.7和PySide创建了一个应用程序。由于这个应用程序相当大,我不得不创建许多自定义库。这些库有时需要从其他自定义库调用方法,因此我让库中的类继承其他库的类。然而,这有时会以继承解析问题而告终,这使得我不得不创建冗余和不优雅的解决方案作为解决方法。你知道吗
现在,我正在从头开始重写应用程序,我想确保我的自定义库的设计不依赖于继承。通常最好的方法是什么?你知道吗
我想我应该用with
:
from libs import envlib
from libs import diskoplib
from libs import dblib
class Something(object):
def __init__(self, parent=None):
super(Something, self).__init__()
def do_something(self):
names = None
filepath = None
id = None
with envlib.Query() as e,
diskoplib.Query() as d,
dblib.Query() as db:
id = e.get_opened_file_id()
names = db.get_names()
filepath = d.get_filepath()
print id, names, filepath # ready to be used for something
这通常是一个好的方法,还是有理由认为这可能不是一个好的方法?你知道吗
创建类属性是一个更好的主意。感谢@jonsharpe和@JohnKeyes的建议。你知道吗
相关问题 更多 >
编程相关推荐