repetitive functions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • trihaitran
    New Member
    • Feb 2008
    • 7

    repetitive functions

    Hi,

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

    [code=python]
    def func(arg):
    statement1
    statement2
    statement3

    unique code

    statement4
    statement5
    [/code]

    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?
  • Subsciber123
    New Member
    • Nov 2006
    • 87

    #2
    Originally posted by trihaitran
    Hi,

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

    [code=python]
    def func(arg):
    statement1
    statement2
    statement3

    unique code

    statement4
    statement5
    [/code]

    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?
    Yep, this shouldn't be too difficult.
    [CODE=python]def foo1(string):
    return string+"baked beans "

    def foo2(string):
    return string+"spam "

    def func(second_fun c,arg):
    arg+="eggs "
    arg=second_func (arg)
    arg+="and spam"
    return arg

    func(foo1,"spam ")
    "spam eggs baked beans and spam"

    func(foo2,"spam ")
    "spam eggs spam and spam"
    [/code]

    Comment

    Working...