This is a Python style question -- my Python code works, I'm just looking for suggestions for a coding convention that would make the code easier to read/understand/debug.
Specifically, I'm working on a Python class that lets the caller add widgets to a custom GUI. To set up the GUI, the user would write a method that adds widgets (named or anonymous) to the widget area so that the widgets form a tree (as is common in GUIs).
In order to allow the user to set up a tree of widgets without having to give a name to every container widget (and then explicitly reference that parent widget every time a child widget is added), my API supports the notion of a "parent widget stack". When declaring a container widget, the user can specify to push that widget onto this stack, and then any further widgets (that don't explicitly specify a parent) will be added to the parent at the top of the stack by default. Here's a simple example of what I mean:
def SetupGUI(self):
self.AddWidget(name="root", type="container", push=True)
self.AddWidget(type="container", push=True)
for i in range(0,8):
self.AddWidget(name="button%i"%i, type="button")
self.PopParentWidget() # pop the buttons-container off the parents-stack
self.AddWidget(type="container", push=True)
for i in range(0,8):
self.AddWidget(name="slider%i"%i, type="slider")
self.PopParentWidget() # pop the sliders-container off the parents-stack
self.PopParentWidget() # pop the container "root" off the parents-stack
This is convenient, but I find that when the GUI hierarchy gets more elaborate, it starts to become difficult to tell which call to self.PopParentWidget() corresponds to which container widget. It's easy to put in one too many, or one too few, and end up with highly amusing but unintended results in the GUI.
So my question is, short of forcing PopParentWidget() to take an explicit widget name (which I want to avoid, because I don't want to have to name every container widget), is there anything I can do to make the push/pop pairing in the code more apparent to the eye?
In C/C++ I would do it using indentation, but with Python I'm not allowed to do that. For example, I'd love to be able to do this:
def SetupGUI(self):
self.AddWidget(name="root", type="container", push=True)
self.AddWidget(type="container", push=True)
for i in range(0,8):
self.AddWidget(name="button%i"%i, type="button")
self.PopParentWidget() # pop the buttons-container off the parents-stack
self.AddWidget(type="container", push=True)
for i in range(0,8):
self.AddWidget(name="slider%i"%i, type="slider")
self.PopParentWidget() # pop the sliders-container off the parents-stack
self.PopParentWidget() # pop the container "root" off the parents-stack
... but Python will throw up an IndentationError if I get creative like that.