{"id":5363,"date":"2014-03-30T21:07:10","date_gmt":"2014-03-30T21:07:10","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/does-python-2-5-have-an-equivalent-to-tcls-uplevel-command-collection-of-common-programming-errors\/"},"modified":"2014-03-30T21:07:10","modified_gmt":"2014-03-30T21:07:10","slug":"does-python-2-5-have-an-equivalent-to-tcls-uplevel-command-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/does-python-2-5-have-an-equivalent-to-tcls-uplevel-command-collection-of-common-programming-errors\/","title":{"rendered":"does python 2.5 have an equivalent to Tcl&#39;s uplevel command?-Collection of common programming errors"},"content":{"rendered":"<p>In general, what you ask is not possible (with the results you no doubt expect). E.g., imagine the &#8220;any code&#8221; is <code>x = 23<\/code>. Will this add a new variable <code>x<\/code> to your caller&#8217;s set of local variables, assuming you do find a black-magical way to execute this code &#8220;in the caller&#8221;? No it won&#8217;t &#8212; <strong>the<\/strong> crucial optimization performed by the Python compiler is to define once and for all, when <code>def<\/code> executes, the exact set of local variables (all the barenames that get assigned, or otherwise bound, in the function&#8217;s body), and turn every access and setting to those barenames into very fast indexing into the stackframe. (You <em>could<\/em> systematically defeat that crucial optimization e.g. by having an <code>exec ''<\/code> at the start of every possible caller &#8212; and see your system&#8217;s performance crash through the floor in consequence).<\/p>\n<p><em>Except<\/em> for assigning to the caller&#8217;s local barenames, <code>exec thecode in thelocals, theglobals<\/code> may do roughly what you want, and the <code>inspect<\/code> module lets you get the locals and globals of the caller in a semi-reasonable way (in as far as deep black magic &#8212; which would make me go postal on any coworker suggesting it be perpetrated in production code &#8212; can ever be honored with the undeserved praise of calling it &#8220;semi-reasonable&#8221;, that is;-).<\/p>\n<p>But you do specify &#8220;I want to be able to execute any code.&#8221; and the only solution to <em>that<\/em> unambiguous specification (and thanks for being so precise, as it makes answering easier!) is: then, use a different programming language.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In general, what you ask is not possible (with the results you no doubt expect). E.g., imagine the &#8220;any code&#8221; is x = 23. Will this add a new variable x to your caller&#8217;s set of local variables, assuming you do find a black-magical way to execute this code &#8220;in the caller&#8221;? No it won&#8217;t [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5363","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5363","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5363"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5363\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}