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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:35:09+00:00 2026-06-17T07:35:09+00:00

I am going through the How to make a chain of function decorators? to

  • 0

I am going through the How to make a chain of function decorators?
to understand decorator.

In the following example, we see that “method_to_decorate” is accessible to wrapper function because of closures.
But, I didn’t understand how arguments self and lie are accessible to the wrapper function.

def method_friendly_decorator(method_to_decorate):
     def wrapper(self, lie):
         lie = lie - 3 # very friendly, decrease age even more :-)
         return method_to_decorate(self, lie)
     return wrapper

class Lucy(object):

    def __init__(self):
        self.age = 32

    @method_friendly_decorator
    def sayYourAge(self, lie):
        print "I am %s, what did you think?" % (self.age + lie)

l = Lucy()
l.sayYourAge(-3)
#outputs: I am 26, what did you think?
  • 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-06-17T07:35:11+00:00Added an answer on June 17, 2026 at 7:35 am

    The returned wrapper replaces the decorated function, and is thus treated as a method. The original sayYourAge took (self, lie) and so does the new wrapper.

    So, when calling l.sayYouAge(-3) you are really calling the nested function wrapper, which is by that time a bound method. Bound methods get self passed in, and -3 is assigned to the argument lie. wrapper calls method_to_decorate(self, lie), passing these arguments on to the original decorated function.

    Note that self and lie are hardcoded into the wrapper() signature; it is tightly bound to the decorated function. These were not taken from the decorated function, the programmer who wrote the wrapper knew beforehand what arguments would be expected of the wrapped version. Note that the wrapper doesn’t have to match arguments with the decorated function at all.

    You could add arguments, for example:

    def method_friendly_decorator(method_to_decorate):
         def wrapper(self, lie, offset=-3):
             lie += offset # very friendly, adjust age even more!
             return method_to_decorate(self, lie)
         return wrapper
    

    Now you can make Lucy lie about her age in different ways:

    l.sayYourAge(-1, offset=1)  # will say "I am 32, what did you think?"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am going through a tutorial, and it showed this example code to make
I've been going through the process of cleaning up our controller code to make
Going through happstack-lite tutorial : we build functions that have return type of ServerPart
Going through Javascript documentation, I found the following two functions on a Javascript object
Going through some documentation on modifying CGImageRef data, I came across a strange example
While going through SWig generated wrappers, I find that the PInvokes don't have any
I'm going through some silverlight apps and reviewing date formatting to make sure the
I was going through some of the decisions made to make Xara Xtreme ,
I been going through this tutorial http://www.codeproject.com/KB/linq/BulkOperations_LinqToSQL.aspx and them make a SP like this
I am going through some pl/sql code with no comments. Trying to make sense

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.