vendredi 17 juillet 2015

Accessing nested attributes in large projects

In my attempt to build my first large cross-platform app I'm running into some trouble in trying to access deeply nested attributes. Trying to build good habits I thought I'd ask here on SO what is considered to be the best practice for accessing attributes that will be shared over multiple classes for large projects.

I've broken the main sections of my GUI up into different classes so I can reorder the GUI easily if needed and in an attempt to build a more logical structure/for readability.

An example:

class OptionBar(tk.Frame):

     #init, write methods, etc

#Other classes for large sections of GUI
......

class Main(tk.Frame):

    def __init__(self, parent, *args, **kwargs):

        #init attributes

    #write methods

if __name__ == "__main__":
     root = tk.Tk()
     app = Main(root)
     ......

An example of accessing a nested attribute in my own current project

app.option_bar.file_extension.menu.ext_choice

To get the selected file extension in the file_extension ttk ComboBox

Having to do this seems like a design flaw, but personally I don't think it's all too terrible for readability, yes it gets long, but tells me exactly what I'm accessing / checking.

So, for large projects is it considered better to make a class to hold commonly shared attributes? An example of my commonly shared attributes would be a queue, threads, tkinter widgets like optionmenu's etc. This seems like it would be more trouble than it's worth to implement or is accessing nested attributes like this considered acceptable?

Aucun commentaire:

Enregistrer un commentaire