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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:20:54+00:00 2026-05-16T06:20:54+00:00

As of 2.4 (2.6 for classes), python allows you to decorate a function with

  • 0

As of 2.4 (2.6 for classes), python allows you to decorate a function with another function:

def d(func): return func

@d
def test(first): pass

It’s a convenient syntactic sugar. You can do all sorts of neat stuff with decorators without making a mess. However, if you want to find out the original function that got decorated you have to jump through hoops (like Cls.method.__func__.__closure__[0].cell_contents or worse).

I found myself wishing for a better way and found that there had been some discussion on python-dev about adding a variable called __decorated__ to the [new] function returned by the decorator. However, it appears that didn’t go anywhere.

Being an adventuresome person, and having had pretty heavy python experience for about 4 years, I thought I would look into implementing __decorated__ in the python compiler source, just to see how it goes.

To be honest I have never delved into the C underneath the hood, so my first hours have been just trying to make sense of how the underlying C code works. So firstly, what would be the best resources to get my head around what I would have to change/add for __decorator__?

Secondly, if a decorator returns a new function then __decorated__ would just return the original, decorated function. However, if the decorator returns the original function, what should happen? Here are three options I could think of (the third is my favorite):

  1. Don’t add __decorator__.
  2. Add __decorator__ but set it to None.
  3. Add __decorator__ and set it to the original function anyway.

So if it were to happen, what do you think would be the best option?

UPDATE:

Someone else brought to my attention a scenario that I had missed. What happens when the decorator returns neither the original function nor a function that wraps the original? At that point nothing is holding a reference to the original function and it will get garbage collected. (Thanks Oddthinking!)

So in that case, I think that I would still go with the third option. The object returned by the decorator would gain a __decorated__ name that references the original function. This would mean that it would not be garbage-collected.

It seems weird to me that the function from a class definition would utterly disappear because you decorated it. In my mind that is even more reason to have a __decorated__ attribute applied for every decorator. However, it’s more likely that my intuition is faulty and that the current behavior is what most people would expect. Any thoughts?

p.s. this is an extension of an earlier, more general question I had. I also went for more info on the first part with a separate post.

  • 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-16T06:20:54+00:00Added an answer on May 16, 2026 at 6:20 am

    Well, independent of any discussion over whether this is a good idea, I’d go for option #3 because it’s the most consistent: it always shows that the function has been decorated by the presence of the attribute, and accessing the value of the attribute always returns a function, so you don’t have to test it against None.

    You should also consider this, though: what would you propose to do about manual decoration? e.g.

    def test(first): pass
    test = d(test)
    

    Regarding the first part of the question, I haven’t looked at the Python interpreter source code very much so I wouldn’t be able to point you to anything particularly useful.

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

Sidebar

Related Questions

setattr allows you to dynamically name attributes in Python classes. I'm trying to do
I'm trying to extend some base classes in Python: class xlist (list): def len(self):
Python allows aliasing of imports, through ...as <ALIAS> clauses in the import statement, like
I've found a strange issue with subclassing and dictionary updates in new-style classes: Python
Possible Duplicate: How many Python classes should I put in one file? Coming from
I achieved to expose some C++ classes to python with the help of boost.
I'm using new style classes in Python 2.6 and am having trouble with __getattr__
I'm trying to write a finalizer for Python classes that have circular references. I
My doc strings have references to other python classes that I've defined. Every time
Hi I am having some issues wrapping some ZMQ pull clients in Python classes.

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.