I've got about 10 functions that all share a bit of code. They all look something like this:
def func(arg): statement1 statement2 statement3 unique code statement4 statement5
Each function does similar statements at the beginning and end, but has unique stuff in the middle. Is there a way for me to simplify this as not to repeat code?
I suppose the direct attack would look like:
def common_func(arg, sub_func): statement1 statement2 statement3 if sub_func==1: code for option 1 elif sub_func==2: code for option 2 etc. statement4 statement5
Then if you like:
my_first_func = lambda x: common_func(x,1) my_other_func = lambda x: common_func(x,2)
(or something like that, I forget the exact lambda syntax)
You would then call
my_other_func(arg) and nobody would know they are both executing the same underlying code.
Use the "template method" software design pattern!
Here's how it works: we're going to split that function of yours across two different functions in two different classes: a superclass with a "template method" and a subclass with a "hook method." The template method in the superclass contains all the common code - all those numbered statements in your example. The hook method in the subclass contains the unique code. The template method class will call the hook method in the subclass. Then, you'll create an instance of the subclass and call its template method (which it inherits). The whole thing works out brilliantly, as this sterling example will show:
# yomama.py insults your mother # This is the template class, used for common code class YoMamaTemplate: # This is the template method def yo_mama(self): # Common code print "YO MAMA" # Call the 'hook' which is defined in the subclass self.print_condition() # You can have as many hooks as you want! self.print_punchline() # More common code print "OH SNAP" # This is the subclass, used for the unique code class YoMamaSoUgly(YoMamaTemplate): # Here is the 'hook' method, with specific code def print_condition(self): print "SO UGLY" # And here is another - you can have as many as you like def print_punchline(self): print "SHE LOOKED OUT THE WINDOW AND " "GOT ARRESTED FOR MOONING" # Create an instance of the subclass y = YoMamaSoUgly() # But call the template method of the superclass! y.yo_mama()
See that? The variable 'y' refers to an instance of the subclass, so it has access to both the template method and the hook method. When we call the template method on y, it has everything it needs to run the whole routine. What's really nice about the template method is that it partitions common code from a family of similar functions, so that if you ever have to add a new variant, you just make a new subclass. For example, suppose I want to insult your mother's intelligence, in addition to her looks. In that case, I don't have to do anything to the superclass - I just make a new subclass, like so:
# This is another subclass class YoMamaSoStupid(YoMamaTemplate): # Here is the 'hook' method, with specific code def print_condition(self): print "SO STUPID" # And here is another - you can have as many as you like def print_punchline(self): print "SHE STOPPED AT A STOP SIGN AND " "WAITED FOR IT TO SAY GO" # Create an instance of the subclass y = YoMamaSoStupid() # But call the template method of the superclass! y.yo_mama()
Hope this helps!
Please tell me why triangle shape is redrawing on mouse move. It want triangle shape draw only once until mouse release.
Here is my code below. Thank you ...
I want to draw a triangle using mouse events like mousePressed(), mouseReleased() and mouseDragged(). But triangle did not show until mouse release. Please tell me what i am ...
Please I have an autocomplete textbox which fetches value from a tabe in the database. But after i choose the name i want to get the id of the value ...