Note that there are some explanatory texts on larger screens.

plurals
  1. PO__decorated__ for python decorators
    primarykey
    data
    text
    <p>As of 2.4 (2.6 for classes), python allows you to decorate a function with another function:</p> <pre><code>def d(func): return func @d def test(first): pass </code></pre> <p>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 <code>Cls.method.__func__.__closure__[0].cell_contents</code> or worse).</p> <p>I found myself wishing for a better way and found that there had been some discussion on python-dev about adding a variable called <code>__decorated__</code> to the [new] function returned by the decorator. However, it appears that didn't go anywhere.</p> <p>Being an adventuresome person, and having had pretty heavy python experience for about 4 years, I thought I would look into implementing <code>__decorated__</code> in the python compiler source, just to see how it goes. </p> <p>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, <strong>what would be the best resources to get my head around what I would have to change/add for <code>__decorator__</code>?</strong></p> <p>Secondly, if a decorator returns a new function then <code>__decorated__</code> 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):</p> <ol> <li>Don't add <code>__decorator__</code>.</li> <li>Add <code>__decorator__</code> but set it to None.</li> <li>Add <code>__decorator__</code> and set it to the original function anyway.</li> </ol> <p>So if it were to happen, <strong>what do you think would be the best option?</strong> </p> <p>UPDATE:</p> <p>Someone else <a href="https://stackoverflow.com/questions/3375573/finding-a-functions-parameters-in-python/3501316#3501316">brought to my attention</a> 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 <a href="https://stackoverflow.com/users/8014/oddthinking">Oddthinking</a>!)</p> <p>So in that case, I think that I would still go with the third option. The object returned by the decorator would gain a <code>__decorated__</code> name that references the original function. This would mean that it would not be garbage-collected. </p> <p>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 <code>__decorated__</code> 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. <strong>Any thoughts?</strong></p> <p>p.s. this is an extension of an earlier, more general <a href="https://stackoverflow.com/questions/3375573/finding-a-functions-parameters-in-python">question</a> I had. I also went for more info on the first part with a <a href="https://stackoverflow.com/questions/3505029/extending-the-code-of-python-adding-language-features">separate</a> post. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload