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 6114575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:59:26+00:00 2026-05-23T14:59:26+00:00

For a decorator I am writing I would like to manipulate a specific named

  • 0

For a decorator I am writing I would like to manipulate a specific named parameter of a function. Consider the following decorator:

def square_param(param):
    def func_decorator(func):
        def func_caller(*args,**kwargs):
            kwargs[param] = kwargs[param] * kwargs[param]
            return func(*args,**kwargs)
    return func_caller
return func_decorator

Applied on the next function:

@square_param('dividend')
def quotient(divisor=1,dividend=0):
    return dividend/divisor

This will work if dividend is called as a keyword argument e.g.:

>>> quotient(dividend=2)
4

However, when given as a positional argument this will fail.

>>> quotient(3,4)
TypeError: quotient() got multiple values for keyword argument 'dividend'

With Python 3 I could solve this by forcing the parameter to be always given as a keyword:

@square_param('dividend')
def quotient(divisor=1,*,dividend=0):
    return dividend/divisor

but I would like to support Python 2 and also I would like to put as little restrictions on the function.

Is there a way that I can fix this behaviour in my decorator?

  • 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-23T14:59:27+00:00Added an answer on May 23, 2026 at 2:59 pm

    Firstly, your square_param decorator doesn’t work because it doesn’t return the functions. It needs to be:

    def square_param(param):
        def func_decorator(func):
            def func_caller(*args,**kwargs):
                kwargs[param] = kwargs[param] * kwargs[param]
                return func(*args,**kwargs)
            return func_caller
        return func_decorator
    

    Now I took @Dirk’s advice and looked into the inspect module. You can do it by checking first if the parameter is one of the function’s positional arguments, and second if that positional argument has been specified, and then modifying that argument position. You also need to make sure you only modify kwargs if the parameter was supplied as a keyword argument.

    import inspect
    
    def square_param(param):
        def func_decorator(func):
            def func_caller(*args,**kwargs):
                funparams = inspect.getargspec(func).args
                if param in funparams:
                    i = funparams.index(param)
                    if len(args) > i:
                        args = list(args)   # Make mutable
                        args[i] = args[i] * args[i]
                if param in kwargs:
                    kwargs[param] = kwargs[param] * kwargs[param]
                return func(*args,**kwargs)
            return func_caller
        return func_decorator
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way of writing a decorator such that the following would work?
The @cache_page decorator is awesome. But for my blog I would like to keep
I've got the following problem: I need to write a decorator that would be
I am writing a function decorator that will apply a conversion to the first
I am trying to write a decorator to do logging: def logger(myFunc): def new(*args,
I am writing a small site decorator to make my local airport site work
I'm writing an Eclipse plug-in... In my custom label decorator, I want to overlay
I would like my application to download and install Silverlight 4, NOT Silverlight 5.
If I set the logging module to DEBUG with a command line parameter like
I'm writing a little cached function in a plugin / library. It takes a

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.