用实例方法组织代码在大片里不好吗pythonclass

Is it bad to use instance methods to organize code in a large python class

下面我有一个伪代码,其中定义了实例方法主要是为了代码组织。 init 函数按特定顺序设置了对各种实例方法的调用链,我才开始觉得以这种方式组织代码是不好的做法。

如果有,是否有任何关于组织大型 classes(例如 GUI 应用程序中的那些)以提高其可读性的参考资料?

假人代码:

class App:
    def __init__(self):
        def do_things()

    def do_things(self):
        self.do_a_thing()
        self.do_another_thing()

    def do_a_thing(self):
        self.thing1 = 1    

    def do_another_thing(self):
        self.thing2 = self.thing1 + 1

这些函数只会在 class 被调用时执行一次,那么这是否表明它们不应该是函数(或实例方法)?

我选择了这种可能很糟糕的组织方法的工作示例

下面,fill() 方法类似于 do_things(),例如init_scroll()init_lb() 类似于 do_a_thing()do_another_thing(),等等

import Tkinter as tk
import tkFont

class EditorApp:
    def __init__( self, master) :
        self.root = master
        self.root.title('Editor')
        self.main = tk.Frame( self.root )
        self.main.pack(fill=tk.BOTH, expand=True)

        self.fill()

##################
# ADDING WIDGETS #
##################
    def fill( self): 
        self.canvas = tk.Canvas( self.main )
        self.canvas.pack(fill=tk.BOTH, expand=tk.YES)
        self.init_scroll()
        self.init_lb()
        self.pack_config_scroll()
        self.pack_bind_lb()
        self.fill_listbox()

##############
# SCROLLBARS #
##############
    def init_scroll(self):
        self.scrollbar  = tk.Scrollbar(self.canvas, orient="vertical")
        self.xscrollbar = tk.Scrollbar(self.canvas, orient="horizontal")

    def pack_config_scroll(self):
        self.scrollbar.config(command=self.lb.yview)
        self.xscrollbar.config(command=self.xview)
        self.scrollbar.pack(side="right", fill="y")
        self.xscrollbar.pack(side="bottom", fill="x")

    def onMouseWheel(self, event):
        self.lb.yview("scroll", event.delta,"units")
        return "break"

    def xview(self, *args):
        self.lb.xview(*args)

################
# MAIN LISTBOX #
################
    def init_lb( self):
        self.lb = tk.Listbox(self.canvas, 
                            font=tkFont.Font(self.canvas, 
                                             family="Courier", 
                                             size=14),
                            yscrollcommand=self.scrollbar.set,   
                            xscrollcommand=self.xscrollbar.set, 
                            exportselection=False)

    def pack_bind_lb(self):
        self.lb.pack(fill="both", expand=True)
        self.lb.bind("<MouseWheel>", self.onMouseWheel)

    def fill_listbox(self):
        dummie_lines = [ 'line%d\t'%x+ ''.join(['AllWorkAndNoPlayMakesJackADullBoy']*100) for x in xrange(50) ]
        for line in  dummie_lines:
            self.lb.insert(tk.END, line) 

if __name__ == '__main__':
    root   = tk.Tk()
    editor = EditorApp(root)
    root.mainloop() 

为了调试,我试图将类似的方法组合在一起(我从中提取的代码是 class,它有 300 行),但我对公认的样式指南知之甚少.

此外,完整的申请已发布here

您是否担心开发人员可能会独立于 do_things 调用 do_thing?虽然 Python 不支持私有方法,但您可以使用前导下划线约定指示 do_thing 应被视为私有方法:

def _do_thing(self):
    ...

这告诉开发人员不要直接调用 _do_thing

通常,将大型方法或函数分解为更小、更易于管理的方法或函数是一个很好的主意。这在单元测试代码时尤其重要。