{"id":1652,"date":"2023-07-13T16:11:25","date_gmt":"2023-07-13T08:11:25","guid":{"rendered":"http:\/\/zhang.mba\/?p=1652"},"modified":"2023-07-13T16:11:46","modified_gmt":"2023-07-13T08:11:46","slug":"python-zhong-deeval-han-shu","status":"publish","type":"post","link":"https:\/\/zhang.mba\/index.php\/2023\/07\/13\/16\/11\/25\/1652\/python-zhong-deeval-han-shu\/uncategorized\/zhangzhiqi\/","title":{"rendered":"Python\u4e2d\u7684eval()\u51fd\u6570"},"content":{"rendered":"<p>eval() \u662fPython\u5185\u7f6e\u51fd\u6570\u4e4b\u4e00\uff0c\u7528\u4e8e\u8bc4\u4f30\u5e76\u6267\u884c\u4ee5\u5b57\u7b26\u4e32\u5f62\u5f0f\u8868\u793a\u7684Python\u8868\u8fbe\u5f0f\u3002\u5b83\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">eval(expression, globals=None, locals=None)\n<\/code><\/pre>\n<p>\u53c2\u6570\u8bf4\u660e\uff1a<\/p>\n<p>expression \u662f\u8981\u8bc4\u4f30\u7684\u5b57\u7b26\u4e32\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u5305\u542b\u6709\u6548\u7684Python\u4ee3\u7801\u3002<br \/>\nglobals\uff08\u53ef\u9009\uff09\u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u63d0\u4f9b\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u3002\u5982\u679c\u672a\u63d0\u4f9b\uff0c\u5c06\u4f7f\u7528\u5f53\u524d\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u3002<br \/>\nlocals\uff08\u53ef\u9009\uff09\u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u63d0\u4f9b\u5c40\u90e8\u547d\u540d\u7a7a\u95f4\u3002\u5982\u679c\u672a\u63d0\u4f9b\uff0c\u5c06\u4f7f\u7528\u5f53\u524d\u5c40\u90e8\u547d\u540d\u7a7a\u95f4\u3002<br \/>\neval() \u51fd\u6570\u5c06\u89e3\u6790\u5e76\u6267\u884c\u4f20\u9012\u7ed9\u5b83\u7684\u5b57\u7b26\u4e32\u8868\u8fbe\u5f0f\uff0c\u5e76\u8fd4\u56de\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u3002\u5b83\u53ef\u4ee5\u7528\u4e8e\u6267\u884c\u7b80\u5355\u7684\u7b97\u672f\u8fd0\u7b97\u3001\u8c03\u7528\u51fd\u6570\u6216\u65b9\u6cd5\u3001\u521b\u5efa\u5bf9\u8c61\u7b49\u3002\u4e0b\u9762\u662f\u51e0\u4e2a\u793a\u4f8b\u8bf4\u660e eval() \u51fd\u6570\u7684\u7528\u6cd5\uff1a<\/p>\n<p>\u7b80\u5355\u8868\u8fbe\u5f0f\u6c42\u503c\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">result = eval(&quot;2 + 3 * 4&quot;)  # \u6267\u884c\u8868\u8fbe\u5f0f\uff1a2 + 3 * 4\nprint(result)  # \u8f93\u51fa\u7ed3\u679c\uff1a14\n<\/code><\/pre>\n<p>\u8c03\u7528\u51fd\u6570\u6216\u65b9\u6cd5\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">x = 5\ny = eval(&quot;pow(x, 2)&quot;)  # \u8c03\u7528 pow() \u51fd\u6570\u8ba1\u7b97 x \u7684\u5e73\u65b9\nprint(y)  # \u8f93\u51fa\u7ed3\u679c\uff1a25\n<\/code><\/pre>\n<p>\u521b\u5efa\u5bf9\u8c61\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">class MyClass:\n    def __init__(self, value):\n        self.value = value\n\nobj = eval(&quot;MyClass(42)&quot;)  # \u521b\u5efa MyClass \u5bf9\u8c61\uff0c\u5e76\u4f20\u9012\u53c2\u6570 42\nprint(obj.value)  # \u8f93\u51fa\u7ed3\u679c\uff1a42\n<\/code><\/pre>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0ceval() \u51fd\u6570\u6267\u884c\u4f20\u9012\u7684\u5b57\u7b26\u4e32\u8868\u8fbe\u5f0f\u65f6\uff0c\u4f1a\u5c06\u5176\u4f5c\u4e3aPython\u4ee3\u7801\u8fdb\u884c\u6c42\u503c\u3002\u56e0\u6b64\uff0c\u5982\u679c\u5b57\u7b26\u4e32\u8868\u8fbe\u5f0f\u6765\u81ea\u4e0d\u53d7\u4fe1\u4efb\u7684\u6765\u6e90\uff0c\u4f7f\u7528 eval() \u65f6\u5e94\u8c28\u614e\uff0c\u4ee5\u9632\u6b62\u6267\u884c\u6076\u610f\u4ee3\u7801\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6700\u597d\u907f\u514d\u4f7f\u7528 eval()\uff0c\u9664\u975e\u5b8c\u5168\u4fe1\u4efb\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u8868\u8fbe\u5f0f\u3002<\/p>\n<!--CusAds0-->\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>eval() \u662fPython\u5185\u7f6e\u51fd\u6570\u4e4b\u4e00\uff0c\u7528\u4e8e\u8bc4\u4f30\u5e76\u6267\u884c\u4ee5\u5b57\u7b26\u4e32\u5f62\u5f0f\u8868\u793a\u7684Python\u8868\u8fbe\u5f0f\u3002\u5b83\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0},"categories":[1],"tags":[61],"_links":{"self":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts\/1652"}],"collection":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/comments?post=1652"}],"version-history":[{"count":0,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts\/1652\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/media?parent=1652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/categories?post=1652"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/tags?post=1652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}