0

我在 Python/PyQt4 中制作了一个超级简单的 Notepad++ 克隆,我想知道这些存储编辑器选项卡数据的选项中的哪一个:

选项 1:我有一个名为 QQCodeTab 的类,它存储当前选项卡的当前 Qsci.QsciScintilla 实例、文件路径、当前语言等。这些通过 dict 映射到选项卡索引。

选项 2:与选项 1 相同,但删除类并将所有内容存储在 dict 中(例如{1: {"scintilla": <blah>, "filepath": "C:/File/whatevs.py"}, "language": "python"}:)

我的代码注释可以更好地解释它。

from PyQt4 import QtGui, Qsci

class QQCodeEditor(QtGui.QTabWidget):
    def __init__(self, parent=None):
        QtGui.QTabWidget.__init__(self, parent)
        self.new_tab()
        self.new_tab()
        # Option 1: Maps index to tab object
        # Option 2: Maps index to dict of options
        self.tab_info = {}

    def new_tab(self):
        scin = Qsci.QsciScintilla()
        index = self.addTab(scin, "New Tab")

    def get_tab_info(self, index):
        # Returns QQCodeTab object
        return self.tab_info[index]

    def save(self, index):
        # Option 2: Save dialog boc and file system stuff goes here
        pass

class QQCodeTab(object):
    def __init__(self, scintilla, editor):
        self.scintilla = scintilla
        self.editor = editor

    def save(self):
        # Option 1: Save dialog box and file system stuff goes here
        pass
4

1 回答 1

0

If you're wondering whether to use a class of dictionary, you probably want a namedtuple. That gives you the simplicity of a dict with the attribute syntax of a class:

from collections import namedtuple

FooBar = namedtuple("FooBar", ["these", "are", "the", "attributes"])

FooBar(123, 324, the=12, attributes=656).these
#>>> 123
于 2013-09-27T21:33:58.723 回答