repetitive functions

Thread Solved

Join Date: Dec 2006
Posts: 28
Reputation: trihaitran is an unknown quantity at this point 
Solved Threads: 0
trihaitran trihaitran is offline Offline
Light Poster

repetitive functions

 
0
  #1
Feb 20th, 2008
Hi,

I've got about 10 functions that all share a bit of code. They all look something like this:

  1. def func(arg):
  2. statement1
  3. statement2
  4. statement3
  5.  
  6. unique code
  7.  
  8. statement4
  9. 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?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 311
Reputation: BearofNH is on a distinguished road 
Solved Threads: 40
BearofNH's Avatar
BearofNH BearofNH is offline Offline
Posting Whiz

Re: repetitive functions

 
0
  #2
Feb 20th, 2008
I suppose the direct attack would look like:
  1. def common_func(arg, sub_func):
  2. statement1
  3. statement2
  4. statement3
  5.  
  6. if sub_func==1:
  7. code for option 1
  8. elif sub_func==2:
  9. code for option 2
  10. etc.
  11.  
  12. statement4
  13. statement5
Then if you like:
  1. my_first_func = lambda x: common_func(x,1)
  2. my_other_func = lambda x: common_func(x,2)
(or something like that, I forget the exact lambda syntax)

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
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 146
Reputation: G-Do is an unknown quantity at this point 
Solved Threads: 28
G-Do's Avatar
G-Do G-Do is offline Offline
Junior Poster

Re: repetitive functions

 
0
  #3
Feb 20th, 2008
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:
  1. # yomama.py insults your mother
  2.  
  3. # This is the template class, used for common code
  4. class YoMamaTemplate:
  5.  
  6. # This is the template method
  7. def yo_mama(self):
  8.  
  9. # Common code
  10. print "YO MAMA"
  11.  
  12. # Call the 'hook' which is defined in the subclass
  13. self.print_condition()
  14.  
  15. # You can have as many hooks as you want!
  16. self.print_punchline()
  17.  
  18. # More common code
  19. print "OH SNAP"
  20.  
  21. # This is the subclass, used for the unique code
  22. class YoMamaSoUgly(YoMamaTemplate):
  23.  
  24. # Here is the 'hook' method, with specific code
  25. def print_condition(self):
  26. print "SO UGLY"
  27.  
  28. # And here is another - you can have as many as you like
  29. def print_punchline(self):
  30. print "SHE LOOKED OUT THE WINDOW AND "
  31. "GOT ARRESTED FOR MOONING"
  32.  
  33. # Create an instance of the subclass
  34. y = YoMamaSoUgly()
  35.  
  36. # But call the template method of the superclass!
  37. 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:
  1. # This is another subclass
  2. class YoMamaSoStupid(YoMamaTemplate):
  3.  
  4. # Here is the 'hook' method, with specific code
  5. def print_condition(self):
  6. print "SO STUPID"
  7.  
  8. # And here is another - you can have as many as you like
  9. def print_punchline(self):
  10. print "SHE STOPPED AT A STOP SIGN AND "
  11. "WAITED FOR IT TO SAY GO"
  12.  
  13. # Create an instance of the subclass
  14. y = YoMamaSoStupid()
  15.  
  16. # But call the template method of the superclass!
  17. y.yo_mama()
Hope this helps!
Last edited by G-Do; Feb 20th, 2008 at 5:53 pm. Reason: Needed to freshen the call-out
Vi veri veniversum vivus vici
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC