On Mon, Apr 11, 2011 at 08:43:25AM +0800, leechau wrote: > Hi everybody. I wrote a script to run BVT and it's code structure like this:
What's BVT? > def step1: > local_var1 = ... > # some other variable definitions for step1 > def substep11: > pass > def substep12: > pass > # more substeps [...] > # more steps ... > global_var1 = ... > # more global var definitions... Using global variables is generally considered an anti-pattern and poor design. You should rethink your design and try to avoid globals. > if step1.return_success: > step2 > step3 > ... > stepN This doesn't work with the skeleton you provide. In the skeleton provided, step1 is a function object. Since you never call the function, it doesn't do anything. Likewise, it doesn't have an attribute called "return_success". > As you see, this script consists of serveral steps(a list of commands), > and a step often consists of several substeps. I used to put all the > steps in one module, where one step corresponding to one funciton and > one substep corresponding to one nested function. It's now a headache to > maintain this script because of totally 1000+ lines of code. 1000 lines of code isn't much. If it's a headache, that's more likely due to poor design (globals?) than volume. > Meanwhile > when I manipulate different projects with this script I need to modify > the value of some variables and detailed task logic. Exactly! And now you know why global variables are frowned on. > Obviously it's a > dirty work to modify the source code directly for different projects. So > I wonder if I should put different steps in different modules to shorten > the length of each module? If I should put the default value of a > variable and high level task logic in a abstract class, and define > specific value and detailed task logic in concrete subclass? Is there > any design patterns available for this kind of multi-steps scripts? Except the most trivial scripts, all scripts are multi-step. You should use argument passing instead of globals. Your functions should take arguments, and return results, instead of reading and setting globals. -- Steven _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor