Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 343251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:52:48+00:00 2026-05-12T10:52:48+00:00

I’m actually struggling with some piece of code. I do know that it can

  • 0

I’m actually struggling with some piece of code. I do know that it can be refactored, but I can’t find the nice-smart-elegant solution.

Here are two functions (much more functions of that kind are in my code):

def fooA(param1, param2):
    if param2 == True:
       code_chunk_1

    fooA_code  #uses only param1

    if param2 == True:
       code_chunk_2


def fooB(param1, param2):
    if param2 == True:
       code_chunk_1

    fooB_code  #uses only param1

    if param2 == True:
       code_chunk_2

My first idea was to use this decorator:

def refactorMe(func):
    def wrapper(*args):
        if args[-1]:
            code_chunk_1

        func(*args)

        if args[-1]:
            code_chunk_2

    return wrapper

And finally:

@refactorMe
def fooA(param1, param2):
    fooA_code  #uses only param1

@refactorMe
def fooB(param1, param2):
    fooB_code  #uses only param1

Unfortunately, I’m not happy with this solution:

  • This decorator is “intrusive” and specific to the fooA & fooB functions
  • param2 is not used anymore in the fooA & fooB body, but we must keep it in the function signature

Perhaps I’m not using the decorator for its initial purpose?

Is there any other way to refactor the code?

Thanks a lot!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T10:52:48+00:00Added an answer on May 12, 2026 at 10:52 am

    What you are describing is a situation where you have some boilerplate, some behaviour, followed by some boiler plate. Essentially a situation where you could use a Higher Order Function (like map, reduce or filter).

    You could do what Ned suggests (though, I’d use functools.partial rather than defining fooA/fooB longhand):

    import functools
    
    ...
    
    fooA = functools.partial(call_one, _fooA)
    fooB = functools.partial(call_one, _fooB)
    

    … but that effectively gets you back to the same place as with your decorator, introducing some clutter into the namespace along the way.

    You could rewrite your decorator to allow functions that only take one parameter, but return functions that take two:

    def refactorMe(func):
        def wrapper(parm1, parm2):
            if parm1:
                code_chunk_1
    
            func(parm1)
    
            if parm2[-1]:
                code_chunk_2
    
        return wrapper
    

    Getting rid of the star magic is an improvement as this decorator is not general to all functions so we should be explicit about it. I like the fact that we change the number of parameters less as anyone looking at the code could easily be confused by the fact that when we call the function we are adding an extra parameter. Furthermore it just feels like decorators that change the signature of the function they decorate should be bad form.

    In summary:

    Decorators are higher order functions, and templating behaviour is precisely what they’re for.

    I would embrace the fact that this code is specific to your fooXXX functions, by making the decorator internal and having it take precisely the number of arguments needed (because foo(*args, **kwargs) signatures makes introspection a pain).

    def _refactorMe(func):
            @functools.wraps(func) #the wraps decorator propagates name/docsting
            def wrapper(parm1, parm2):
                if parm1:
                    code_chunk_1
    
                func(parm1, parm2)
    
                if parm2:
                    code_chunk_2
    
            return wrapper
    

    I’d leave the calls taking two parameters, even though one is unused just so that the decorator doesn’t change the signature. This isn’t strictly necessary as if you document the functions as they look after decoration and you are restricting the use of the decorator to this small set of functions then the fact that the signature changes shouldn’t be that big a deal.

    @_refactorMe
    def fooB(param1, param2):
        fooB_code  #uses only param1
    
    
    @_refactorMe
    def fooB(param1, param2):
        fooB_code  #uses only param1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to clean up various Word 'smart' characters in user input, including but
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i want to parse a xhtml file and display in UITableView. what is the
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.