{"id":4437,"date":"2021-06-23T12:51:44","date_gmt":"2021-06-23T07:21:44","guid":{"rendered":"https:\/\/blog.guvi.in\/?p=4437"},"modified":"2025-10-28T14:40:45","modified_gmt":"2025-10-28T09:10:45","slug":"why-is-using-eval-a-bad-practice","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/why-is-using-eval-a-bad-practice\/","title":{"rendered":"Why is using &#8216;eval&#8217; a Bad Practice?"},"content":{"rendered":"\n<p>Are you using the <strong><em>eval<\/em><\/strong> function to evaluate the mathematical expression in Python? Yes, many of you might be. <em>But do you even know that using eval can be a bad practice?&nbsp;<\/em> Now, if you are wondering why? Then probably you should stick around and understand this, with a couple of good examples, and get a solution for it.<\/p>\n\n\n\n\n\n<h2 class=\"wp-block-heading\"><em><strong>Why is using &#8216;eval&#8217; a bad practice?&nbsp;<\/strong><\/em><\/h2>\n\n\n\n<p>It has been seen that the eval function <strong>starts deleting the system files and corrupts the system&#8217;s environment<\/strong>. Therefore, whenever you use the eval() function for executing the user input code, be careful.<\/p>\n\n\n\n<p>Moreover, make sure that you must check the user-entered data first, and when it looks fine only then go for it.<\/p>\n\n\n\n<p>Well, this blog will help you to find out the other reasons why eval can be a bad practice. Moreover, we have tried to detail the alternative functions that you can use at the place of eval.<\/p>\n\n\n\n<p>Do not worry. We have mentioned relevant examples with the details so that it will be more easy for you to understand the eval() function.<\/p>\n\n\n\n<p>Additionally, if you want to explore Python through a self-paced course, try <a href=\"https:\/\/www.guvi.in\/courses\/programming\/python\/?utm_source=blog&amp;utm_medium=organic&amp;utm_campaign=why-is-using-eval-a-bad-practice\"><strong>HCL GUVI\u2019s Python self-paced course<\/strong><\/a>.<\/p>\n\n\n\n<p>Now that we understand why is using <em>eval <\/em>a bad practice, let&#8217;s find out the essential details about the eval function.<\/p>\n\n\n\n<p><strong>First, let&#8217;s take an overview of <\/strong><strong><em>the eval <\/em><\/strong><strong>function in Python!<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong><em>What is an eval function in Python?<\/em><\/strong><\/h2>\n\n\n\n<p><strong><em>eval <\/em><\/strong>is one of the built-in functions in Python. It parses the given argument and evaluates the expressions.&nbsp;<\/p>\n\n\n\n<p>Or simply, I can say that the<strong> <em>eval <\/em>function in Python evaluates the &#8220;<em>string<\/em>&#8221; <\/strong>as the expression and returns the output result as the &#8220;<em>integer<\/em>&#8220;.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Key point:<\/strong><br>The <em>eval <\/em>function of Python takes the string as &#8220;argument,&#8221; and evaluates it as the Python &#8220;expression.&#8221; The result of the Python expression is an &#8220;object.&#8221;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The basic syntax of the <em>eval <\/em>function is:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/ZCQwNVA-TQMF4XJ7qgkRNCz6PjRenGvKxywvF375KFKgrpHPg5UGSP2V16SgusGL_Bd5-246XErd_kFcagQtLvxT3KE6nS0SwdCBckvWdPUw6wrTdXD0fFIb3_PcfEDxC_NGVC0\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong><em>expression:<\/em><\/strong> The string that is evaluated as the Python expression.<strong><em>globals (optional):<\/em><\/strong> The dictionary that specifies the global variables and methods.<strong><em>locals (optional):<\/em><\/strong> The dictionary that specifies the local variables and methods.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong><em>What is the basic use of the eval function?<\/em><\/strong><\/h2>\n\n\n\n<p>This function uses when there is a requirement to evaluate mathematical expressions. Moreover, Python users use it to evaluate the string into codes.<\/p>\n\n\n\n<p>It is possible just because the <strong><em>eval <\/em><\/strong>function is evaluating the <strong><em>string expressions<\/em><\/strong><strong> <\/strong>and returning the result in the form of an <strong><em>integer<\/em><\/strong>.<\/p>\n\n\n\n<p><strong>Now, let&#8217;s take an example of how to use the <em>eval<\/em> function in Python!<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/ahaG2XRWTLrRyF2cHRaSO1tPn9y0gaoD70VRbp1R05q5ghV7HYPhwlQqGxlhQQcmAGdC1RlSCSGLEi6MIhJXJB3WtXjPm0si4oeOYuV_VV-Xr4ZouUUiSCCDGNT3STyB7Aolcmo\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Input<\/strong><\/h3>\n\n\n\n<p>\u2018x**2\u2019<\/p>\n\n\n\n<p>\u20183\u2019<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Output:<\/strong><\/h3>\n\n\n\n<p>Enter the equation(in terms of x):x**2<\/p>\n\n\n\n<p>&lt;type &#8216;str&#8217;&gt;<\/p>\n\n\n\n<p>Enter the value of x:3<\/p>\n\n\n\n<p>y = 9<\/p>\n\n\n\n<p>&lt;type &#8216;int&#8217;&gt;<\/p>\n\n\n\n<p><strong>NOTE: function_creator <\/strong>is one of those functions that evaluate the user-created mathematical functions.<\/p>\n\n\n\n<p>Moreover, you can see that the input is given as the string, and the output of the <em>eval <\/em>function is in integer form.<\/p>\n\n\n\n<p><strong>Now, let&#8217;s find out the reason why and how <\/strong><strong><em>&#8216;eval&#8221; <\/em><\/strong><strong>can be a bad practice!!<\/strong><\/p>\n\n\n\n<p>In the above code, you can see that there is a function_creator<strong> <\/strong>that has few limitations. Like the user can call the function to get the hidden value of the program. This will happen because the <em>eval <\/em>just executes whatever is passing to it.<\/p>\n\n\n\n<p><strong>It will look like this:<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Input:<\/strong><\/h3>\n\n\n\n<p>&#8216;secret_function()&#8217;<\/p>\n\n\n\n<p>&#8216;0&#8217;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Output:<\/strong><\/h3>\n\n\n\n<p>Enter the equation(in terms of x):secret_function()<\/p>\n\n\n\n<p>Enter the value of x:0<\/p>\n\n\n\n<p>y = Secret key is 159<\/p>\n\n\n\n<p>This issue can also occur if you import the OS module in any of the Python programs. The portable way of OS allows us to utilize OS functionalities, such as write or read a file.&nbsp;<\/p>\n\n\n\n<p>It is quite dangerous to use as its single command can delete almost all the system files!!<\/p>\n\n\n\n<p>Now, you might be thinking <em>why do I use it then? <\/em>Well, while writing the Python script for some applications like kiosk computers, web apps, and more, you need to take a risk to use it.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Sum up: <\/strong><em>eval <\/em>can also be a bad practice because of the following reasons:<br>It is insecure and very dangerous. It is quite slow. Makes the process of debugging quite difficult.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong><em>Then, why do Python users still use the eval function?<\/em><\/strong><\/h2>\n\n\n\n<p>Well, <em>eval <\/em>is not much in use as you might be thinking just because of security reasons.<\/p>\n\n\n\n<p>But in some situation, you may need to use it, like:<\/p>\n\n\n\n<ul>\n<li>When you allow other users to enter their &#8220;scriptlets,&#8221; which use to customize the complex system&#8217;s behavior.<\/li>\n<\/ul>\n\n\n\n<ul>\n<li>The <em>eval function <\/em>is practiced to evaluate mathematical expressions.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong><em>Is there any way to make eval safe or use any other function instead?<\/em><\/strong><\/h2>\n\n\n\n<p><em>Yes, there is!!<\/em><\/p>\n\n\n\n<p><a href=\"https:\/\/www.guvi.in\/blog\/do-you-know-how-to-create-variables-in-python\/\">Python users can pass the list of variables<\/a> and functions as arguments to a dictionary.<\/p>\n\n\n\n<p><em>How?<\/em><\/p>\n\n\n\n<p><strong>Let&#8217;s check it in the below code:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/fyrQWsz9Yh2r1-hFBq77M9dWZwn70l5HAjY1zaOMyVbWiiVFKIXCTzkMOJf0pAjkgQck-cV_HU-2r8iWhpd8j4LivC2bqoPRqB_YvTitxoopqE1C5sfkTXkH83Ua1h9gqt7z0Lw\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Input:&nbsp;<\/strong><\/h3>\n\n\n\n<p>&#8216;x**2&#8217;<\/p>\n\n\n\n<p>&#8216;3&#8217;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Output:<\/strong><\/h3>\n\n\n\n<p>Enter the equation(in terms of x):x**2<\/p>\n\n\n\n<p>&lt;type &#8216;str&#8217;&gt;<\/p>\n\n\n\n<p>Enter the value of x:3<\/p>\n\n\n\n<p>y = 9<\/p>\n\n\n\n<p>&lt;type &#8216;int&#8217;&gt;<\/p>\n\n\n\n<p>Now, when you enter the input as secret_function() and the value of x as 0, then the output will be different from the previous case.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Input:<\/strong><\/h3>\n\n\n\n<p>&#8216;secret_function()&#8217;<\/p>\n\n\n\n<p>&#8216;0&#8217;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Output:<\/strong><\/h3>\n\n\n\n<p>Enter the equation(in terms of x):secret_function()<\/p>\n\n\n\n<p>&lt;type &#8216;str&#8217;&gt;<\/p>\n\n\n\n<p>Enter the value of x:0<\/p>\n\n\n\n<p>NameError: name &#8216;secret_function&#8217; is not defined<\/p>\n\n\n\n<p><strong>Now, let&#8217;s understand what is happening here.<\/strong><\/p>\n\n\n\n<p>First, we have created a safe dictionary method where &#8220;<strong><em>keys&#8221; <\/em><\/strong>are the names and their namespace is <strong><em>&#8220;values&#8221;.<\/em><\/strong><\/p>\n\n\n\n<p><strong><em>safe_dict = dict([(k, locals().get(k, None)) for k in safe_list])<\/em><\/strong><\/p>\n\n\n\n<p>Here, <strong><em>locals() <\/em><\/strong>returns the dictionary that uses to map the variables and methods in the local scope along with the namespaces.<\/p>\n\n\n\n<p><strong><em>safe_dict[&#8216;x&#8217;] = x<\/em><\/strong><\/p>\n\n\n\n<p>Keep in mind that you need to add <strong><em>x <\/em><\/strong>to the safe_dict so that other variables other than x need to be identified by the <strong><em>eval <\/em><\/strong>function<em>.<\/em><\/p>\n\n\n\n<p><strong><em>y = eval(expr, {&#8220;__builtins__&#8221;:None}, safe_dict)<\/em><\/strong><\/p>\n\n\n\n<p>Now, <strong><em>eval <\/em><\/strong>takes global and local variables&#8217; dictionaries as arguments. To ensure that there is no built-in method available, we have passed other dictionaries from the <strong><em>safe_dict <\/em><\/strong>function as well.<\/p>\n\n\n\n<p>This is how you can easily make the eval safe to use.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong><em>Conclusion<\/em><\/strong><\/h2>\n\n\n\n<p>So, we hope we are able to coil up multiple reasons and probable solutions to the obvious question that we started with- Why is using eval a bad practice? With relevant examples, we have seen that using <em>eval <\/em>can be a bad practice, but still it is in use. However, if possible, try to use the <em><strong>dict <\/strong><\/em>function instead of the <em>eval <\/em>function. On the other hand, if there is an unavoidable requirement of using eval, then do not hesitate to use it.<\/p>\n\n\n\n<p>If you have any query regarding the use of t<em>he eval <\/em>function or anything related to the <em>eval<\/em>, comment it in the below section. I will help you with solving your Python-related queries in the best possible way.<\/p>\n\n\n\n<p><strong><em>&#8220;<a href=\"https:\/\/www.guvi.in\/courses\/programming\/python\/?utm_source=blog&amp;utm_medium=organic&amp;utm_campaign=why-is-using-eval-a-bad-practice\">Master Python with HCL GUVI<\/a> &amp; <a href=\"https:\/\/www.guvi.in\/courses\/programming\/python\/?utm_source=blog&amp;utm_medium=organic&amp;utm_campaign=why-is-using-eval-a-bad-practice\">Earn IIT Certification in Python Programming<\/a>.&#8221;<\/em><\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong><em>Test what you have learned from this blog!!!<\/em><\/strong><\/h2>\n\n\n\n<div class=\"wp-block-group has-background is-layout-flow wp-block-group-is-layout-flow\" style=\"background-color:#d7e1e6\"><div class=\"wp-block-group__inner-container\">\n<ol>\n<li><strong>Is the statement correct or incorrect?<\/strong><\/li>\n<\/ol>\n\n\n\n<p>&#8220;Eval can accept the <em>expressions, <\/em>an error will arise if you use if, while, def, class, for with eval.&#8221;<\/p>\n\n\n\n<p>(A) Correct<\/p>\n\n\n\n<p>(B) Incorrect<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Correct Answer: <\/strong>(A) it is correct that eval can only accept the expression.&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group is-layout-flow wp-block-group-is-layout-flow\"><div class=\"wp-block-group__inner-container\">\n<div class=\"wp-block-group has-background is-layout-flow wp-block-group-is-layout-flow\" style=\"background-color:#d7e1e6\"><div class=\"wp-block-group__inner-container\">\n<ol start=\"2\">\n<li><strong>Is the statement correct or incorrect?<\/strong><\/li>\n<\/ol>\n\n\n\n<p>&#8220;If I remove all the builtins and the global, then eval will become safe to use.&#8221;<\/p>\n\n\n\n<p>(A) Correct<\/p>\n\n\n\n<p>(B) Incorrect<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Correct Answer: <\/strong>(B) it is incorrect because after removing builtins and globals, eval is still a bad practice.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n<\/div><\/div>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group has-background is-layout-flow wp-block-group-is-layout-flow\" style=\"background-color:#d7e1e6\"><div class=\"wp-block-group__inner-container\">\n<ol start=\"3\">\n<li><strong>If you have the string input that matches with the dictionary object. Which function would be better to use?<\/strong><\/li>\n<\/ol>\n\n\n\n<p>(A) Generator expression<\/p>\n\n\n\n<p>(B) Eval<\/p>\n\n\n\n<p>(C) Both (A) and (B)<\/p>\n\n\n\n<p>(D) None of above<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Correct Answer: <\/strong>(B) Because <a href=\"https:\/\/www.guvi.in\/blog\/python-generators-and-comprehensions\/\" target=\"_blank\" rel=\"noreferrer noopener\">generator expression<\/a> can be used when you have string input but it <em>does not <\/em>match with the dictionary objects. Therefore, the eval function can be used instead.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group is-layout-flow wp-block-group-is-layout-flow\"><div class=\"wp-block-group__inner-container\">\n<div class=\"wp-block-group has-background is-layout-flow wp-block-group-is-layout-flow\" style=\"background-color:#d7e1e6\"><div class=\"wp-block-group__inner-container\">\n<ol start=\"4\">\n<li><strong>What will be the output of this code:<\/strong><\/li>\n<\/ol>\n\n\n\n<p><em>x = 3**2<\/em><\/p>\n\n\n\n<p><em>print(eval(&#8216;x + 1&#8217;))<\/em><\/p>\n\n\n\n<p>(A) 4<\/p>\n\n\n\n<p>(B) 6<\/p>\n\n\n\n<p>(C) 7<\/p>\n\n\n\n<p>(D) 10<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Correct Answer: <\/strong>(D) first, x has the value of the square of 3 that is 9, then eval will evaluate the math operation that is 9+1 =&gt;10 and give output as 10.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n<\/div><\/div>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group has-background is-layout-flow wp-block-group-is-layout-flow\" style=\"background-color:#d7e1e6\"><div class=\"wp-block-group__inner-container\">\n<ol start=\"5\">\n<li><strong>What will be the output of the following code:<\/strong><\/li>\n<\/ol>\n\n\n\n<p><em>x = 5<\/em><\/p>\n\n\n\n<p><em>y = 2<\/em><\/p>\n\n\n\n<p><em>print(eval(&#8216;x + y&#8217;))<\/em><\/p>\n\n\n\n<p>(A) 7<\/p>\n\n\n\n<p>(B) 10<\/p>\n\n\n\n<p>(C) 3<\/p>\n\n\n\n<p>(D) Error<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Correct Answer: <\/strong>(A) as the eval is evaluating the \u201caddition\u201d mathematical operation. Therefore x + y = 5 + 2 =&gt; 7.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Are you using the eval function to evaluate the mathematical expression in Python? Yes, many of you might be. But do you even know that using eval can be a bad practice?&nbsp; Now, if you are wondering why? Then probably you should stick around and understand this, with a couple of good examples, and get [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":4438,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37,717],"tags":[],"views":"4190","authorinfo":{"name":"Archana","url":"https:\/\/www.guvi.in\/blog\/author\/archana\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2021\/06\/why-is-using-eval-a-bad-function--300x184.png","jetpack_featured_media_url":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2021\/06\/why-is-using-eval-a-bad-function-.png","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/4437"}],"collection":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=4437"}],"version-history":[{"count":14,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/4437\/revisions"}],"predecessor-version":[{"id":91489,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/4437\/revisions\/91489"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/4438"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=4437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=4437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=4437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}