{"id":4419,"date":"2019-05-14T22:21:32","date_gmt":"2019-05-14T14:21:32","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=4419"},"modified":"2019-05-16T11:20:42","modified_gmt":"2019-05-16T03:20:42","slug":"python%e7%9a%84%e4%b8%80%e4%ba%9b%e5%b0%8f%e7%9f%a5%e8%af%86%e7%82%b9_13","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/4419.html","title":{"rendered":"Python\u7684\u4e00\u4e9b\u5c0f\u77e5\u8bc6\u70b9_13"},"content":{"rendered":"\n<p>=Start=<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7f18\u7531\uff1a<\/h4>\n\n\n\n<p>\u6574\u7406\u603b\u7ed3\u4e00\u4e0b\u6700\u8fd1\u5728\u7f16\u7801\u8fc7\u7a0b\u4e2d\u9047\u5230\u7684\u4e00\u4e9bPython\u77e5\u8bc6\u70b9\uff0c\u65b9\u4fbf\u4ee5\u540e\u5feb\u901f\u53c2\u8003\u3001\u5b66\u4e60\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6b63\u6587\uff1a<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n\n\n\n<h6 class=\"wp-block-heading\">1\u3001Python\u4e2d\u5982\u4f55\u83b7\u53d6\u8c03\u7528\u8005\u7684\u51fd\u6570\u540d<\/h6>\n\n\n\n<pre class=\"wp-block-preformatted\">#!\/usr\/bin\/env python\n# coding=utf-8\n\nimport time\nimport sys\n\n# \u5efa\u8bae\u4f7f\u7528\u4e0b\u9762\u8fd9\u4e2a\u51fd\u6570\uff0c\u4e00\u65b9\u9762\u901f\u5ea6\u5feb\uff0c\u4e00\u65b9\u9762\u53ea\u9700\u8981\u5f15\u5165sys\u6a21\u5757\u5373\u53ef\ndef whoami(): \n    return sys._getframe(1).f_code.co_name\n\ndef foo():\n    print(whoami())\n\ndef caller():\n    print(\"begin caller()\")\n    print(whoami()) # caller\n    print(\"end caller()\")\n\ndef caller2():\n    foo() #foo\n    print(whoami()) #caller2\n\ndef main():\n    caller()\n    foo() #foo\n    caller2()\n\nif __name__ == '__main__':\n    time_start = time.time()\n    try:\n        main()\n    except KeyboardInterrupt:\n        print('Killed by user')\n        sys.exit(0)\n    print(\"Spend {0} seconds.\\n\".format(time.time() - time_start))<\/pre>\n\n\n\n<p>\u5bf9\u5e94\u7684\u6d4b\u8bd5\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ python -m timeit -s 'import inspect, sys' 'inspect.stack()[0][0].f_code.co_name'\n 1000 loops, best of 3: 583 usec per loop\n$ python -m timeit -s 'import inspect, sys' 'inspect.stack()[0][3]'\n 1000 loops, best of 3: 583 usec per loop\n$ python -m timeit -s 'import inspect, sys' 'inspect.currentframe().f_code.co_name'\n 10000000 loops, best of 3: 0.159 usec per loop\n$ python -m timeit -s 'import inspect, sys' 'sys._getframe().f_code.co_name'\n 10000000 loops, best of 3: 0.156 usec per loop<\/pre>\n\n\n\n<h6 class=\"wp-block-heading\">2\u3001Python\u4e2d\u5982\u4f55\u8fdb\u884cdict\u5408\u5e76<\/h6>\n\n\n\n<ul class=\"wp-block-list\"><li>Python 3.5\u53ca\u4ee5\u4e0a\u7248\u672c<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">z = {**x, **y}<\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>Python 2\u6216Python 3.4\u53ca\u4ee5\u4e0b\u7248\u672c<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">def merge_two_dicts(x, y):\n     z = x.copy()   # start with x's keys and values\n     z.update(y)    # modifies z with y's keys and values &amp; returns None\n     return z\n z = merge_two_dicts(x, y)<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/stackoverflow.com\/questions\/5067604\/determine-function-name-from-within-that-function-without-using-traceback\">https:\/\/stackoverflow.com\/questions\/5067604\/determine-function-name-from-within-that-function-without-using-traceback<\/a><\/li><li><a href=\"https:\/\/stackoverflow.com\/questions\/33162319\/how-can-get-current-function-name-inside-that-function-in-python\">https:\/\/stackoverflow.com\/questions\/33162319\/how-can-get-current-function-name-inside-that-function-in-python<\/a><\/li><li><a href=\"https:\/\/stackoverflow.com\/questions\/38987\/how-to-merge-two-dictionaries-in-a-single-expression\">https:\/\/stackoverflow.com\/questions\/38987\/how-to-merge-two-dictionaries-in-a-single-expression<\/a><\/li><li><a href=\"https:\/\/segmentfault.com\/a\/1190000010567015\">https:\/\/segmentfault.com\/a\/1190000010567015<\/a><\/li><\/ul>\n\n\n\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u6574\u7406\u603b\u7ed3\u4e00\u4e0b\u6700\u8fd1\u5728\u7f16\u7801\u8fc7\u7a0b\u4e2d\u9047\u5230\u7684\u4e00\u4e9bPython\u77e5\u8bc6\u70b9\uff0c\u65b9\u4fbf\u4ee5\u540e\u5feb\u901f\u53c2\u8003\u3001\u5b66\u4e60\u3002 \u6b63 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7],"tags":[1386,8,1385],"class_list":["post-4419","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-inspect","tag-python","tag-sys"],"views":3874,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4419","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=4419"}],"version-history":[{"count":3,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4419\/revisions"}],"predecessor-version":[{"id":4427,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4419\/revisions\/4427"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=4419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=4419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=4419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}