| | |
repetitive functions
Thread Solved |
•
•
Join Date: Dec 2006
Posts: 28
Reputation:
Solved Threads: 0
Hi,
I've got about 10 functions that all share a bit of code. They all look something like this:
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've got about 10 functions that all share a bit of code. They all look something like this:
python Syntax (Toggle Plain Text)
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:
Then if you like:
(or something like that, I forget the exact lambda syntax)
You would then call
Python Syntax (Toggle Plain Text)
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
Python Syntax (Toggle Plain Text)
my_first_func = lambda x: common_func(x,1) my_other_func = lambda x: common_func(x,2)
You would then call
my_first_func(arg) or my_other_func(arg) and nobody would know they are both executing the same underlying code. Last edited by BearofNH; Feb 20th, 2008 at 1:36 pm. Reason: typo in sub_func
Hi trihaitran,
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: 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: Hope this helps!
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:
python Syntax (Toggle Plain Text)
# 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()
python Syntax (Toggle Plain Text)
# 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()
Last edited by G-Do; Feb 20th, 2008 at 5:53 pm. Reason: Needed to freshen the call-out
Vi veri veniversum vivus vici
![]() |
Similar Threads
- Projects for the Beginner (Python)
- Tutorial: Understanding ASP classes (ASP)
- Help explain how to write algorithm (Computer Science)
- Help Setting up website and creating domain controller (Windows NT / 2000 / XP)
- A Tricky Question for You! (PHP)
- c++ functions to compare numbers (C++)
- Practice problem (C++)
- procedure and object-oriented ! (C)
Other Threads in the Python Forum
- Previous Thread: calling a function without changing scope
- Next Thread: python pdf creation help
| Thread Tools | Search this Thread |
accessdenied apache application approximation argv array beginner book builtin calculator change converter countpasswordentry curved dan08 dictionary dynamic edit enter examples file float format function gui homework import inches input java keyboard lapse launcher library line lines linux list lists loop microphone mouse movingimageswithpygame mysql mysqlquery newb number numbers numeric output parameters parsing path phonebook plugin port prime programming projects py2exe pygame pyopengl pysimplewizard python random recursion redirect remote reverse scrolledtext session simple smtp software sprite statictext string strings syntax table tennis terminal text textarea thread threading time tlapse trick tuple tutorial twoup ubuntu unicode unit urllib urllib2 variable wordgame wxpython





