<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Using Decorators to Add Arguments</title>
	<atom:link href="http://www.heikkitoivonen.net/blog/2008/12/02/using-decorators-to-add-arguments/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.heikkitoivonen.net/blog/2008/12/02/using-decorators-to-add-arguments/</link>
	<description>A Finn in Silicon Valley - Adventures in Technology</description>
	<lastBuildDate>Wed, 17 Aug 2011 08:58:27 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
	<item>
		<title>By: Phillip J. Eby</title>
		<link>http://www.heikkitoivonen.net/blog/2008/12/02/using-decorators-to-add-arguments/comment-page-1/#comment-5631</link>
		<dc:creator>Phillip J. Eby</dc:creator>
		<pubDate>Wed, 03 Dec 2008 23:17:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.heikkitoivonen.net/blog/?p=495#comment-5631</guid>
		<description>Note, by the way, that you can always design your context objects so that the only way to use them is via &quot;with&quot;: e.g., &quot;with Context() as ctx:&quot;, where Context.__enter__ returns the real context object, and the Context() itself is just a dummy class that raises errors if you try to do anything with it, calls close() on __exit__.</description>
		<content:encoded><![CDATA[<p>Note, by the way, that you can always design your context objects so that the only way to use them is via &#8220;with&#8221;: e.g., &#8220;with Context() as ctx:&#8221;, where Context.__enter__ returns the real context object, and the Context() itself is just a dummy class that raises errors if you try to do anything with it, calls close() on __exit__.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Heikki Toivonen</title>
		<link>http://www.heikkitoivonen.net/blog/2008/12/02/using-decorators-to-add-arguments/comment-page-1/#comment-5623</link>
		<dc:creator>Heikki Toivonen</dc:creator>
		<pubDate>Wed, 03 Dec 2008 17:36:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.heikkitoivonen.net/blog/?p=495#comment-5623</guid>
		<description>@Brian: Thanks, I will definitely need to take a more careful look of &quot;with&quot;. I dismissed that approach too early I guess, since I haven&#039;t actually used &quot;with&quot; much before (just for locks that already had the needed double underscore methods).

@Cristof: I did consider it (&quot;attach the context to the func object&quot;), but I didn&#039;t like how it looked.

@Love Encounter Flow: I dismissed the inspection approach as being too kludgy.

@Marius: Ah, thanks, I wasn&#039;t aware of that.</description>
		<content:encoded><![CDATA[<p>@Brian: Thanks, I will definitely need to take a more careful look of &#8220;with&#8221;. I dismissed that approach too early I guess, since I haven&#8217;t actually used &#8220;with&#8221; much before (just for locks that already had the needed double underscore methods).</p>
<p>@Cristof: I did consider it (&#8220;attach the context to the func object&#8221;), but I didn&#8217;t like how it looked.</p>
<p>@Love Encounter Flow: I dismissed the inspection approach as being too kludgy.</p>
<p>@Marius: Ah, thanks, I wasn&#8217;t aware of that.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Marius Gedminas</title>
		<link>http://www.heikkitoivonen.net/blog/2008/12/02/using-decorators-to-add-arguments/comment-page-1/#comment-5618</link>
		<dc:creator>Marius Gedminas</dc:creator>
		<pubDate>Wed, 03 Dec 2008 13:43:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.heikkitoivonen.net/blog/?p=495#comment-5618</guid>
		<description>If you write decorators, you really want to set the __module__ of the inner function too.  Things like doctests rely on it.  Best use the functools.wraps():

Here goes a massively-blog-comment-distorted-I-expect example:

&lt;pre&gt;
def ensure_ctx(func):
    @functools.wraps(func)
    def decorate(*args, **kw):
        ...
    return decorate
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>If you write decorators, you really want to set the __module__ of the inner function too.  Things like doctests rely on it.  Best use the functools.wraps():</p>
<p>Here goes a massively-blog-comment-distorted-I-expect example:</p>
<pre>
def ensure_ctx(func):
    @functools.wraps(func)
    def decorate(*args, **kw):
        ...
    return decorate
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Love Encounter Flow</title>
		<link>http://www.heikkitoivonen.net/blog/2008/12/02/using-decorators-to-add-arguments/comment-page-1/#comment-5617</link>
		<dc:creator>Love Encounter Flow</dc:creator>
		<pubDate>Wed, 03 Dec 2008 13:24:58 +0000</pubDate>
		<guid isPermaLink="false">http://www.heikkitoivonen.net/blog/?p=495#comment-5617</guid>
		<description>i agree with the foregoing posters that this looks a lot like a with statement use case. you may want to consider using contextlib/@contextmanager to simplify the definition of your context handler. as concerns the implicit introduction of local variables (which you are solving by putting the affected names into the method signature)---i’ve tried it before, and i believe it can only be done by recompiling the method source code (i.e. you would have to use inspection to grab the text from the *.py file, then do a superficial parse and add lines that introduce your new variables, exec the source text, and put the result back into the class or module from whence it came). the only other possible solution i’m aware of is fiddling with the bytecodes, which may or may not be easier. this complication is a sad side-effect of python’s strategy to consider a name local when there is a local assignment without a global statement and vice versa.</description>
		<content:encoded><![CDATA[<p>i agree with the foregoing posters that this looks a lot like a with statement use case. you may want to consider using contextlib/@contextmanager to simplify the definition of your context handler. as concerns the implicit introduction of local variables (which you are solving by putting the affected names into the method signature)&#8212;i’ve tried it before, and i believe it can only be done by recompiling the method source code (i.e. you would have to use inspection to grab the text from the *.py file, then do a superficial parse and add lines that introduce your new variables, exec the source text, and put the result back into the class or module from whence it came). the only other possible solution i’m aware of is fiddling with the bytecodes, which may or may not be easier. this complication is a sad side-effect of python’s strategy to consider a name local when there is a local assignment without a global statement and vice versa.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Christof</title>
		<link>http://www.heikkitoivonen.net/blog/2008/12/02/using-decorators-to-add-arguments/comment-page-1/#comment-5616</link>
		<dc:creator>Christof</dc:creator>
		<pubDate>Wed, 03 Dec 2008 11:45:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.heikkitoivonen.net/blog/?p=495#comment-5616</guid>
		<description>Not sure you dismissed this option in your article above but maybe just add ctx to the func in the decorator ``func.ctx = Context()``? Usage in the some_func is ``some_func.ctx`` but with the decorator before the ``def some_func`` it makes this kind of obvious where .ctx may come from.

Not sure how to format code in this comment so I put the code here: http://pastebin.com/f6fd359ed

Guess usage of ``with`` may even be better but this seems to work and feels logical in usage I think.</description>
		<content:encoded><![CDATA[<p>Not sure you dismissed this option in your article above but maybe just add ctx to the func in the decorator &#8220;func.ctx = Context()&#8220;? Usage in the some_func is &#8220;some_func.ctx&#8220; but with the decorator before the &#8220;def some_func&#8220; it makes this kind of obvious where .ctx may come from.</p>
<p>Not sure how to format code in this comment so I put the code here: <a href="http://pastebin.com/f6fd359ed" rel="nofollow">http://pastebin.com/f6fd359ed</a></p>
<p>Guess usage of &#8220;with&#8220; may even be better but this seems to work and feels logical in usage I think.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ornitorrinco Enmascarado</title>
		<link>http://www.heikkitoivonen.net/blog/2008/12/02/using-decorators-to-add-arguments/comment-page-1/#comment-5614</link>
		<dc:creator>Ornitorrinco Enmascarado</dc:creator>
		<pubDate>Wed, 03 Dec 2008 09:35:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.heikkitoivonen.net/blog/?p=495#comment-5614</guid>
		<description>I agree with Brian, the &quot;with&quot; statment is a more natural solution.</description>
		<content:encoded><![CDATA[<p>I agree with Brian, the &#8220;with&#8221; statment is a more natural solution.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian Zimmer</title>
		<link>http://www.heikkitoivonen.net/blog/2008/12/02/using-decorators-to-add-arguments/comment-page-1/#comment-5612</link>
		<dc:creator>Brian Zimmer</dc:creator>
		<pubDate>Wed, 03 Dec 2008 06:10:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.heikkitoivonen.net/blog/?p=495#comment-5612</guid>
		<description>I think the solution you want is the &quot;with&quot; statement available in Python 2.5+.  I thought this an interesting post so I &lt;a href=&quot;http://bzimmer.ziclix.com/2008/12/02/using-pythons-with-statement/&quot; rel=&quot;nofollow&quot;&gt;responded&lt;/a&gt; with more details than I felt appropriate in a comment box.</description>
		<content:encoded><![CDATA[<p>I think the solution you want is the &#8220;with&#8221; statement available in Python 2.5+.  I thought this an interesting post so I <a href="http://bzimmer.ziclix.com/2008/12/02/using-pythons-with-statement/" rel="nofollow">responded</a> with more details than I felt appropriate in a comment box.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

