<br><div class="gmail_quote">On Thu, Sep 22, 2011 at 6:11 PM, Nick Coghlan <span dir="ltr"><<a href="mailto:ncoghlan@gmail.com">ncoghlan@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<div class="im">On Fri, Sep 23, 2011 at 9:51 AM, Steven D'Aprano <<a href="mailto:steve@pearwood.info">steve@pearwood.info</a>> wrote:<br>
> With decorator syntax, the scoping rules are obvious and straightforward:<br>
><br>
> a = 10<br>
> @inject(b=a)<br>
> def foo():<br>
>    a = 20<br>
>    return b+a<br>
<br>
</div>Please read the previous thread from June (linked earlier in this<br>
thread). Decorator syntax cannot work without deep magic, because the<br>
compiler *doesn't know* that injected names need to be given special<br>
treatment.<br>
<font class="Apple-style-span" color="#888888"><br></font></blockquote><div><br></div><div> Yes, you're right that this can't be done with today's decorators. That doesn't mean it's impossible. Any of the other changes would also require compiler changes. Instead of @inject being a standard decorator, imagine a new kind of decorator that works as follows:</div>

<div><br></div></div><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><div class="gmail_quote"><div>a = 10</div></div><div class="gmail_quote"><div>$inject(b=a)</div></div>

<div class="gmail_quote"><div>def foo()</div></div><div class="gmail_quote"><div>    a = 20</div></div><div class="gmail_quote"><div>    return b+a</div></div></blockquote><div class="gmail_quote"><div><br></div><div>where the effect of a $inject decorator is that it modifies the behavior of the function as follows:</div>

<div><br></div></div><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><div class="gmail_quote"><div>a = 10</div><div>_inject_ = inject_decorator(b=a)</div><div>def foo():</div>

<div>    locals().update(_inject_)</div><div>    a = 20</div><div>    return b + a</div></div></blockquote><div><br></div><div>Yes, I know that update on locals() won't do the right thing but pretend it does. I think the semantics of this are pretty clear and more obvious than the suggested alternatives. Now what about these $-decorators? As defined here, it's pretty specialized to this one operation. Let's imagine that we define $-decorators to be a special class of decorators that are built into Python. We use $-decorators for cases we would like users to think of as decorators but that do some kind of magic that's beyond the capability of a regular decorator. To the user the $ is a signal to the user that this decorator is a bit more special than usual. Right now, there would only be one $-decorator but it's available next time it's desired to add something that just can't quite be a regular decorator.</div>

<div><br></div><div>A good objection to adding new things to the language is that they increase the load on people trying to learn the language. If I see a syntax like:</div><div><br></div><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;">

<div>def f() [i=i]: pass</div></blockquote><div><br></div><div>I can't imagine how I would find out what that does. Search engines don't readily allow searching on special characters. On the other hand, when I see $inject, I go type "python $inject decorator" into my favorite search engine and whether it ignores the $ or not, I'm fairly likely to get good results. Whether the decorator-like syntax is spelled $inject, @@static or something else, I think it's going to be much easier to figure out and remember what's going on.</div>

<div> </div><div><font face="arial, helvetica, sans-serif">--- Bruce</font><div><font face="arial, helvetica, sans-serif">Follow me: <a href="http://www.twitter.com/Vroo" target="_blank">http://www.twitter.com/Vroo</a> <a href="http://www.vroospeak.com/" target="_blank">http://www.vroospeak.com</a></font></div>

<div><font face="arial, helvetica, sans-serif"><br></font></div></div>