{"id":173291,"date":"2020-03-05T08:24:49","date_gmt":"2020-03-05T00:24:49","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=173291"},"modified":"2020-02-24T09:25:59","modified_gmt":"2020-02-24T01:25:59","slug":"python-implementing-calculator","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-implementing-calculator.html","title":{"rendered":"python\u901a\u8fc7\u95ed\u5305\u5b9e\u73b0\u8ba1\u7b97\u5668\u7684\u529f\u80fd"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86python\u5982\u4f55\u901a\u8fc7\u95ed\u5305\u5b9e\u73b0\u8ba1\u7b97\u5668\u7684\u529f\u80fd,\u6587\u4e2d\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u4ecb\u7ecd\u7684\u975e\u5e38\u8be6\u7ec6\uff0c\u5bf9\u5927\u5bb6\u7684\u5b66\u4e60\u6216\u8005\u5de5\u4f5c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u5b66\u4e60\u4ef7\u503c,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86python\u5982\u4f55\u901a\u8fc7\u95ed\u5305\u5b9e\u73b0\u8ba1\u7b97\u5668\u7684\u529f\u80fd,\u6587\u4e2d\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u4ecb\u7ecd\u7684\u975e\u5e38\u8be6\u7ec6\uff0c\u5bf9\u5927\u5bb6\u7684\u5b66\u4e60\u6216\u8005\u5de5\u4f5c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u5b66\u4e60\u4ef7\u503c,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b<\/p>\n

\u6ee1\u8db3\u95ed\u5305\u7684\u6761\u4ef6\uff1a
\n<\/strong><\/div>\n

1\u3001\u51fd\u6570\u4e2d\u5d4c\u5957\u4e00\u4e2a\u51fd\u6570
\n2\u3001\u5916\u5c42\u51fd\u6570\u7684\u8fd4\u56de\u503c\u662f\u5185\u5c42\u51fd\u6570\u7684\u51fd\u6570\u540d
\n3\u3001\u5185\u5c42\u5d4c\u5957\u51fd\u6570\u5bf9\u5916\u90e8\u4f5c\u7528\u57df\u6709\u4e00\u4e2a\u975e\u5168\u5c40\u53d8\u91cf\u7684\u5f15\u7528<\/p>\n

\u95ed\u5305\u7684\u4f5c\u7528\uff1a<\/strong><\/div>\n

\u5b9e\u73b0\u6570\u636e\u9501\u5b9a<\/p>\n

\u88c5\u9970\u5668\u7684\u4f5c\u7528\uff1a<\/strong><\/div>\n

\u5c31\u662f\u4e3a\u5df2\u7ecf\u5b58\u5728\u7684\u5bf9\u8c61\u6dfb\u52a0\u989d\u5916\u7684\u529f\u80fd\u3002<\/p>\n

\u5e38\u7528\u573a\u666f\uff1a<\/strong><\/div>\n

\u63d2\u5165\u65e5\u5fd7\u3001\u6027\u80fd\u6d4b\u8bd5\u3001\u4e8b\u52a1\u5904\u7406\u3001\u7f13\u5b58\u3001\u6743\u9650\u6821\u9a8c\u7b49<\/p>\n

\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/div>\n
\r\ndef scope(func):\r\n  def calc(a, b):\r\n    try:\r\n      _a = float(a)\r\n      _b = float(b)\r\n    except:\r\n      _a = 0\r\n      _b = 0\r\n    return func(_a, _b)\r\n  return calc\r\n \r\n \r\n@scope\r\ndef add(a, b):\r\n  return a + b\r\n \r\n \r\n@scope\r\ndef sub(a, b):\r\n  return a - b\r\n \r\n \r\n@scope\r\ndef mul(a, b):\r\n  return a * b\r\n \r\n \r\n@scope\r\ndef div(a, b):\r\n  try:\r\n    return a \/ b\r\n  except ZeroDivisionError as error:\r\n    print(error)\r\n    return 0\r\n \r\n \r\n# \u8c03\u7528\u95ed\u5305\u51fd\u6570\r\nres = scope(add)(\"1\", 3)\r\nprint(res)\r\n \r\nres = add(\"1\", \"2\")\r\nprint(res)\r\n \r\nres = sub(\"1\", \"2\")\r\nprint(res)\r\n \r\nres = mul(\"1\", \"2\")\r\nprint(res)\r\n \r\nres = div(\"1\", \"2\")\r\nprint(res)\r\n<\/pre>\n

\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9<\/p>\n","protected":false},"excerpt":{"rendered":"

\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86python\u5982\u4f55\u901a\u8fc7\u95ed\u5305\u5b9e\u73b0\u8ba1\u7b97\u5668\u7684\u529f\u80fd,\u6587\u4e2d\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u4ecb\u7ecd\u7684\u975e\u5e38\u8be6\u7ec6\uff0c\u5bf9\u5927\u5bb6\u7684\u5b66\u4e60\u6216\u8005\u5de5 […]<\/p>\n","protected":false},"author":1893,"featured_media":173293,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-173291","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/173291","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1893"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=173291"}],"version-history":[{"count":1,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/173291\/revisions"}],"predecessor-version":[{"id":173294,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/173291\/revisions\/173294"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/173293"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=173291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=173291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=173291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}