<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>jathanism &#187; eval</title>
	<atom:link href="http://jathan.com/tag/eval/feed/" rel="self" type="application/rss+xml" />
	<link>http://jathan.com</link>
	<description>computers, robots, and other cool things.</description>
	<lastBuildDate>Thu, 20 Oct 2011 01:50:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>On Using eval() in Python</title>
		<link>http://jathan.com/2009/12/19/on-using-eval-in-python/</link>
		<comments>http://jathan.com/2009/12/19/on-using-eval-in-python/#comments</comments>
		<pubDate>Sat, 19 Dec 2009 21:07:00 +0000</pubDate>
		<dc:creator>jathan</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[best practices]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[eval]]></category>
		<category><![CDATA[help]]></category>
		<category><![CDATA[stackoverflow]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://jathan.com/?p=249</guid>
		<description><![CDATA[Originally posted on StackOverflow. I have used eval() in the past (and still do from time-to-time) for massaging data during quick and dirty operations. It is part of the toolkit that can be used for getting a job done, but should NEVER be used for anything you plan to use in production such as any [...]]]></description>
			<content:encoded><![CDATA[<p><em>Originally posted on <a href="http://stackoverflow.com/questions/1933451/why-should-exec-and-eval-be-avoided/1933703#1933703">StackOverflow</a>.</em></p>

<p>I have used <code>eval()</code> in the past (and still do from time-to-time) for massaging data during quick and dirty operations.  It is part of the toolkit that can be used for getting a job done, but should <strong>NEVER be used for anything you plan to use in production</strong> such as any command-line tools or scripts, because of all the reasons mentioned <a href="http://stackoverflow.com/questions/1933451/why-should-exec-and-eval-be-avoided/">in the other answers</a>.  </p>

<p>You cannot trust your users&#8211;ever&#8211;to do the right thing.  In most cases they will, but you have to expect them to do all of the things you never thought of and find all of the bugs you never expected.  This is precisely where <code>eval()</code> goes from being a tool to a liability.</p>

<p>A perfect example of this would be using Django, when constructing a <code>QuerySet</code>.  The parameters passed to a query accepts keyword arguments, that look something like this:</p>

<pre><code>results = Foo.objects.filter(whatever__contains='pizza')
</code></pre>

<p>If you&#8217;re programmatically assigning arguments, you might think to do something like this:</p>

<pre><code>results = eval("Foo.objects.filter(%s__%s=%s)" % (field, matcher, value))
</code></pre>

<p>But there is always a better way that doesn&#8217;t use <code>eval()</code>, which is passing a dictionary by reference:</p>

<pre><code>results = Foo.objects.filter( **{'%s__%s' % (field, matcher): value} ) 
</code></pre>

<p>By doing it this way, it&#8217;s not only faster performance-wise, but also safer and more Pythonic. </p>

<p>Moral of the story?</p>

<p>Use of <code>eval()</code> is <strong>ok</strong> for small tasks, tests, and truly temporary things, but <strong>bad</strong> for permanent usage because there is almost certainly always a better way to do it!</p>
]]></content:encoded>
			<wfw:commentRss>http://jathan.com/2009/12/19/on-using-eval-in-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

