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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:03:48+00:00 2026-05-31T13:03:48+00:00

For the sake of learning, I’m trying to chain decorators that are defined by

  • 0

For the sake of learning, I’m trying to chain decorators that are defined by classes. I read this question about decorators, which has a lot of good information about chaining them with functions. They also link to the documentation, but I’m trying to figure out simpler examples.

Basically, I’m trying to mimic similar behaviour using classes. Here is my first decorator definition, which works perfectly.

class square_result(object):
    def __init__(self, f):
        pass

def __call__(self, x, y):
        return (x+y)**2

@square_result
def add_two_numbers(x, y):
    return x + y

print(add_two_numbers(2,5)) #Outputs 49, as expected

Then, I add another decorator to create this code snippet:

class square_result(object):
    def __init__(self, f):
        pass

    def __call__(self, x, y):
        return (x+y)**2

class append_abc(object):
    def __init__(self, f):
        pass

    def __call__(self, *args):
        return str(*args) + "abc"

@append_abc
@square_result
def add_two_numbers(x, y):
    return x + y

print(add_two_numbers(2,5))
#Ideally, this should print "49abc" but prints "(2,5)abc" instead

what is the proper way of doing this? I guess what I want to do is create a decorator in the form of a class that takes the output of the function it decorates (in this case square_result) and appends "abc" to it.

I know that when I add a decorator to my code, the add_two_numbers() function is compiled and that function object is passed to the square_result class, which does something to create a function-like object which is substituted for the original add_two_numbers(). However, I’m not sure how to chain this.

  • 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-31T13:03:49+00:00Added an answer on May 31, 2026 at 1:03 pm

    This does what you want:

    class square_result(object):
        def __init__(self, f):
            pass
    
        def __call__(self, x, y):
            return (x+y)**2
    
    class append_abc(object):
        def __init__(self, f):
            self.f = f
    
        def __call__(self, *args):
            return str(self.f(*args)) + "abc"
    
    @append_abc
    @square_result
    def add_two_numbers(x, y):
        return x + y
    
    print(add_two_numbers(2,5))
    

    You need to actually run the inner function in the decorator if you want to use its output in the result of the decorator.

    I didn’t edit your first decorator, as it does what you want, but it actually isn’t useful as a decorator. Since its output is not related in any way to the function it’s decorating, it’s just replacing the function. If you wanted to replace the function with that class, the way to do it would be

    class square_result(object):
        def __call__(self, x, y):
            return (x+y)**2
    
    # this has no effect at all after the reassignment    
    def add_two_numbers(x, y):
        return x + y
    
    add_two_numbers = square_result()
    

    PEP8 also suggests CamelCase for your class names (SquareResult).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to make a simple app (just for learning sake) that will be
This question is just for a sake of knowledge. I don't have any practical
I was trying case insensitive pattern matching. For the sake of learning i tried
Learning F# while trying to do something useful at the same time, so this
For the sake of learning, I'm trying to call an Assembly function from C#.
For the sake of learning more about jQuery, I was wondering if anyone could
Just for learning's sake, I tried in java doc but I did not find
For the sake of convenience I am trying to assign multiple values to a
For my curiosity sake I'm looking for a dynamic object oriented language that allows
I am learning multi-threading and for the sake of understanding I have wriiten 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.