{"id":344514,"date":"2021-02-12T05:31:59","date_gmt":"2021-02-11T21:31:59","guid":{"rendered":"http:\/\/4563.org\/?p=344514"},"modified":"2021-02-12T05:31:59","modified_gmt":"2021-02-11T21:31:59","slug":"python-%e5%a6%82%e4%bd%95%e4%bf%ae%e6%94%b9-py-%e6%96%87%e4%bb%b6%e5%86%85%e5%87%bd%e6%95%b0%e7%9a%84-docstring","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=344514","title":{"rendered":"Python \u5982\u4f55\u4fee\u6539 py \u6587\u4ef6\u5185\u51fd\u6570\u7684 docstring"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Python \u5982\u4f55\u4fee\u6539 py \u6587\u4ef6\u5185\u51fd\u6570\u7684 docstring               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : css3 <\/span>  <span><i><\/i> 4<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<h1>\u6709\u8fd9\u4e48\u4e2a\u9700\u6c42\uff0c\u9700\u8981\u7528 py \u811a\u672c\u4fee\u6539\u4e00\u4e9b py \u6587\u4ef6\u5185\u7684\u51fd\u6570 docstring\uff0c \u5e76\u4fdd\u5b58\u8986\u76d6<\/h1>\n<p> <\/p>\n<pre><code>\u251c\u2500\u2500 test.py \u251c\u2500\u2500 modify.py <\/code><\/pre>\n<pre><code># test.py \u5f85\u4fee\u6539 docstring \u7684\u51fd\u6570\u6587\u4ef6  def hello_world():     \"\"\"     this function is a demo\uff0c \u9700\u8981\u4fee\u6539\u8fd9\u91cc\uff0c\u5e76\u4fdd\u5b58\u5230\u6587\u4ef6     \"\"\"     return 'hello world'      \"\"\"     # \u8fd9\u662f\u4e00\u4e2a\u5e72\u6270\u6ce8\u91ca\uff0c\u4e0d\u80fd\u4fee\u6539\u8fd9\u91cc     \"\"\" <\/code><\/pre>\n<pre><code># modify.py \u4fee\u6539 test.py \u7684 docstring \u51fd\u6570  from test import hello_world  def modify_docstring():      print(hello_world.__doc__) # this function is a demo\uff0c \u9700\u8981\u4fee\u6539\u8fd9\u91cc\uff0c\u5e76\u4fdd\u5b58\u5230\u6587\u4ef6      hello_world.__doc__ = 'I' am new docstring'       print(hello_world.__doc__) # I' am new docstring  # \u8fd9\u6837\u53ef\u4ee5\u8d4b\u503c\uff0c\u4f46\u53ea\u662f\u5728\u5185\u5b58\u4e2d\uff0c\u600e\u4e48\u4fee\u6539\u5199\u5165 test.py \u6587\u4ef6\u5462\uff1f  <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>12<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5309579\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : guyeu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u96be\u70b9\u5728\u8bed\u6cd5\u5206\u6790\u91cc\u3002\u3002\u4e0d\u8fc7\u53ea\u662f\u8bc6\u522b docstring \u7684\u8bdd\uff0c\u611f\u89c9\u6b63\u5219\u5c31\u884c\u4e86\u3002\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5309580\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : nthhdy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e3b import \u4e4b\u540e\u76f4\u63a5\u8d4b\u503c docstring \u7684\u65b9\u6cd5\uff0c\u636e\u6211\u6240\u77e5\u505a\u4e0d\u5230\u6539\u6e90\u6587\u4ef6\u7684\u3002<br \/>\u53ef\u4ee5\u8003\u8651\u7528 ast module\uff0cparse \u51fa\u6765\u4e4b\u540e\u6539\uff0c\u7136\u540e\u518d\u751f\u6210\u4ee3\u7801\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5309581\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xchaoinfo <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             ast                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5309582\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : codists <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e2a\u4eba\u8ba4\u4e3a\u8fd9\u6d89\u53ca\u5230\u4e86\u6587\u4ef6\u8bfb\u5199\u3002<\/p>\n<p>&#8220;`python<br \/># modify.py \u4fee\u6539 test.py \u7684 docstring \u51fd\u6570<\/p>\n<p>import re<\/p>\n<p>input_f = open(&#8216;test.py&#8217;, &#8216;r&#8217;)<br \/>txt = re.sub(r'&#8221;&#8221;&#8221;[dD]*?&#8221;&#8221;&#8221;&#8216;, r'&#8221;&#8221;I&#8217; am new docstring&#8221;&#8221;&#8221;&#8216;, input_f.read(), 1)<\/p>\n<p># \u5199\u5165\u5185\u5bb9<br \/>output_f = open(&#8216;test.py&#8217;, &#8216;w&#8217;)<br \/>output_f.write(txt)<\/p>\n<p># \u5173\u95ed\u6587\u4ef6<br \/>input_f.close()<br \/>output_f.close()<\/p>\n<p>&#8220;`                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5309583\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : julyclyde <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u563f\uff0c\u8fd9\u662f\u6253\u7b97\u76d7\u7248\u8c01\u5bb6\u7684\u8f6f\u4ef6\u4ea7\u54c1\u554a                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5309584\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : css3 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @guyeu \u6b63\u5219\u6015\u8bef\u4f24&#8221;\u53cb\u519b&#8221;                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5309585\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : css3 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @nthhdy <br \/>@xchaoinfo \u611f\u8c22\u8001\u54e5\u4eec\uff0c\u5c1d\u8bd5\u4e86\u4e00\u4e0b ast\uff0c\u8c8c\u4f3c\u628a\u539f\u6765\u7684\u591a\u884c doc \u7ed9\u7528&#8217;n&#8217;\u5408\u5e76\u6210\u4e86\u4e00\u884c\u53bb\u4e86\uff0c\u8fd8\u6ca1\u627e\u5230\u89e3\u51b3\u529e\u6cd5<br \/>@julyclyde \u6ca1\u6709\u554a\uff0c\u8001\u54e5<br \/>@codists \u591a\u8c22\u8001\u54e5\uff0c\u4e0d\u4f18\u5148\u8003\u8651\u7528\u6b63\u5219\uff0c\u56e0\u4e3a\u5927\u91cf\u6587\u4ef6\u91cc\u8fb9\u9664\u4e86 docstring \u7528\u4e86\u4e09\u5f15\u53f7\uff0c\u8fd8\u6709\u5927\u91cf\u6ce8\u91ca\uff0c\u6015\u8bef\u4f24\u53cb\u519b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5309586\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : nthhdy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             &gt; \u8c8c\u4f3c\u628a\u539f\u6765\u7684\u591a\u884c doc \u7ed9\u7528&#8217;n&#8217;\u5408\u5e76\u6210\u4e86\u4e00\u884c\u53bb\u4e86\uff0c\u8fd8\u6ca1\u627e\u5230\u89e3\u51b3\u529e\u6cd5<\/p>\n<p>\u611f\u89c9\u8fd9\u4e2a\u60f3\u529e\u6cd5\u89e3\u51b3\u4e00\u4e0b\u5c31\u884c\u4e86\uff0c\u5c31\u5f53\u662f IDE \u7684\u4e00\u4e2a\u5c0f\u529f\u80fd\u5457\uff0c\u633a\u5e38\u89c1\u7684\uff0c\u5e94\u4e0d\u96be\u641e\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5309587\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : css3 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @nthhdy \u6700\u65b0\u53d1\u73b0\u89e3\u6790\u540e\u751f\u6210\u7684 docstring \u7684\u4e09\u4e2a\u5f15\u53f7\u4e5f\u53d8\u6210\u5355\u5f15\u53f7\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5309588\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : css3 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @nthhdy <br \/>@xchaoinfo<br \/>\u8001\u54e5\u4eec\uff0c\u6211\u901a\u8fc7 ast \u6a21\u5757\u8bfb\u53d6 python \u6587\u4ef6\u51fd\u6570\u7684 docstring\uff0c\u7136\u540e\u901a\u8fc7 unpack \u89e3\u6790\u51fa\u6765\uff0c\u53d1\u73b0\u539f\u6765 docstring \u7684\u4e09\u5f15\u53f7\u591a\u884c\u5185\u5bb9\u53d8\u6210\u4e86\u5355\u5f15\u53f7\u5355\u884c\uff0c\u662f\u901a\u8fc7&#8221;n&#8221;\u8fde\u63a5\u6210\u5355\u884c\u7684\uff0c\u8fd9\u8ddf\u6211\u60f3\u8981\u7684\u8fd8\u662f\u6709\u70b9\u5dee\u8ddd\uff0c\u6211\u53ea\u662f\u60f3\u5728\u539f\u6709\u7684 docstring \u4e2d\u589e\u51cf\u4e00\u4e9b\u63cf\u8ff0\uff0c\u4e0d\u9700\u8981\u4e5f\u4e0d\u80fd\u6539\u53d8\u5176\u7ed3\u6784\uff0c\u4f7f\u7528\u7684\u4ee3\u7801\u5b8c\u5168\u590d\u5236\u7684\u6211\u4e0a\u4e2d\u94fe\u63a5\u4e2d stackoverflow \u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5309589\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : nthhdy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u770b\u4e86\u4e00\u4e0b ast \u7684\u6587\u6863\uff0cint float string \u8fd9\u4e9b literal \u5728\u89e3\u6790\u6811\u4e0a\u90fd\u8868\u793a\u4e3a Constant \u5bf9\u8c61\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0cstring \u662f\u5355\u5f15\u53f7\u3001\u53cc\u5f15\u53f7\u8fd8\u662f\u4e09\u4e2a\u5f15\u53f7\uff0c\u8fd9\u4e2a\u4fe1\u606f\u5728\u89e3\u6790\u6811\u4e0a\u5df2\u7ecf\u4e22\u5931\u4e86\u3002\u6240\u4ee5\u4f60\u60f3\u505a\u8fd9\u4ef6\u4e8b\uff0cast \u7684\u786e\u505a\u4e0d\u5230\u3002\u4e0d\u77e5\u9053\u662f\u5426\u6709\u8fb9\u89d2\u7684\u914d\u7f6e\u6216\u8005 hack \u7684\u529e\u6cd5\u53ef\u4ee5\u3002<\/p>\n<p>\u4f46\u8fd9\u4e2a\u601d\u8def\u662f\u6b63\u89e3\uff0c\u5148 parse \u51fa\u6765\uff0c\u505a\u6539\u52a8\uff0c\u518d dump \u6210\u4ee3\u7801\u3002\u6211\u6ca1\u6ce8\u610f\u5230 ast \u7684\u8fd9\u4e2a\u7ec6\u8282\uff0c\u6240\u4ee5\u53ea\u662f\u5de5\u5177\u4e0d\u9002\u5408\uff0c\u53ef\u4ee5\u518d\u641c\u7d22\u4e00\u4e0b\u540c\u7c7b\u578b\u7684\u5de5\u5177\u3002\u8fd9\u4e48\u591a\u505a code format \u7684\u5de5\u5177\u5462\uff0c\u5b83\u4eec\u5e94\u8be5\u5b9e\u73b0\u8fc7\u7c7b\u4f3c\u7684\u673a\u5236\u624d\u5bf9\u3002<\/p>\n<p>\u8fd9\u4e2a\u601d\u8def\u662f\u4e2a\u6bd4\u8f83\u901a\u7528\u7684\u89e3\u6cd5\uff0c\u518d\u590d\u6742\u4e00\u4e9b\u7684\u9700\u6c42\u4e5f\u53ef\u4ee5\u7528\u8fd9\u4e2a\u8def\u5b50\u3002\u5982\u679c\u8981\u89e3\u51b3\u7684\u95ee\u9898\u662f\u4e00\u6b21\u6027\u7684\uff0c\u53d8\u4f53\u53c8\u6709\u9650\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u7528 adhoc \u7684\u529e\u6cd5\u3002\u6bd4\u5982\u7528\u6b63\u5219\uff0c\u751a\u81f3\u5199\u4e00\u4e2a\u81ea\u5df1\u4ee3\u7801\u90fd\u9002\u7528\u7684\u89c4\u5219\u3002\u770b\u95ee\u9898\u7684\u89c4\u6a21\u548c\u590d\u6742\u5ea6\u800c\u5b9a\u5427\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5309590\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : css3 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @nthhdy \u597d\u7684\uff0c\u591a\u8c22\u8001\u54e5\u6307\u70b9\u4e86\uff0c\u8fd8\u662f\u91c7\u7528\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Python \u5982\u4f55\u4fee\u6539 py \u6587\u4ef6&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/344514"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=344514"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/344514\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=344514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=344514"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=344514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}