{"id":384600,"date":"2021-03-11T10:34:10","date_gmt":"2021-03-11T02:34:10","guid":{"rendered":"http:\/\/4563.org\/?p=384600"},"modified":"2021-03-11T10:34:10","modified_gmt":"2021-03-11T02:34:10","slug":"python-%ef%bc%9a%e5%a6%82%e4%bd%95%e9%81%bf%e5%85%8d%e6%9f%90%e4%b8%aa%e5%87%bd%e6%95%b0%e5%86%85%e7%9a%84%e7%b1%bb%e5%8f%8d%e5%a4%8d%e8%bf%9b%e8%a1%8c%e5%ae%9e%e4%be%8b%e5%8c%96%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=384600","title":{"rendered":"Python \uff1a\u5982\u4f55\u907f\u514d\u67d0\u4e2a\u51fd\u6570\u5185\u7684\u7c7b\u53cd\u590d\u8fdb\u884c\u5b9e\u4f8b\u5316\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Python \uff1a\u5982\u4f55\u907f\u514d\u67d0\u4e2a\u51fd\u6570\u5185\u7684\u7c7b\u53cd\u590d\u8fdb\u884c\u5b9e\u4f8b\u5316\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : capbone <\/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<p>\u5047\u8bbe\u6709\u4e00\u4e2a\u5305\u542b\u7c7b <code>C<\/code> \u5b9e\u4f8b\u7684\u51fd\u6570 <code>foo<\/code>\uff1a<\/p>\n<pre><code>def foo(x):     bar = C()     return bar(x) <\/code><\/pre>\n<p><code>foo<\/code> \u8fd9\u4e2a\u51fd\u6570\u4f1a\u88ab\u67d0\u4e2a\u4e1a\u52a1\u53cd\u590d\u8c03\u7528\uff08\u6bd4\u5982\u6bcf\u5206\u949f 10,000 \u6b21\uff09\uff0c\u6709\u4ec0\u4e48\u529e\u6cd5\u80fd\u591f\u8ba9\u7c7b <code>C<\/code> \u7684\u5b9e\u4f8b\u5316\u8fc7\u7a0b\uff08<code>bar = C()<\/code>\uff09\u53ea\u5728\u7b2c\u4e00\u6b21\u88ab\u8c03\u7528\u7684\u65f6\u5019\u6267\u884c\u4e00\u6b21\u5462\uff1f\u6362\u53e5\u8bdd\u8bf4\uff0c\u7b2c\u4e00\u6b21\u8c03\u7528\u751f\u6210 <code>bar<\/code> \u5bf9\u8c61\u540e\uff0c\u5982\u4f55\u628a\u5b83\u7f13\u5b58\u4f4f\uff0c\u8ba9\u7b2c\u4e8c\u6b21\u53ca\u540e\u7eed\u8c03\u7528\u90fd\u76f4\u63a5\u8f7d\u5165\u8fd9\u4e2a\u7f13\u5b58\u5bf9\u8c61\u5462\uff1f<\/p>\n<p>\u6709\u51e0\u4e2a\u9650\u5236\uff1a<\/p>\n<p>1 \u3001<code>C<\/code> \u662f\u5728\u67d0\u4e2a\u7b2c\u4e09\u65b9\u5e93\u4e2d\u5b9e\u73b0\u7684\uff0c\u56e0\u6b64\u6ca1\u6cd5\u4fee\u6539\u6e90\u7801\u8ba9 <code>C<\/code> \u6210\u4e3a\u5355\u4f8b\uff1b<\/p>\n<p>2 \u3001\u51fd\u6570 <code>foo<\/code> \u7684\u53c2\u6570\u683c\u5f0f\u4e0d\u5141\u8bb8\u4fee\u6539\uff1b<\/p>\n<p>3 \u3001<code>foo<\/code> \u53ef\u80fd\u4f1a\u88ab a.py \u8c03\u7528\uff0c\u4e5f\u53ef\u80fd\u88ab b.py \u8c03\u7528\uff0c\u6211\u5e0c\u671b\u5728\u8fd9\u4e24\u4e2a py \u6587\u4ef6\u6267\u884c\u7684\u8fc7\u7a0b\u4e2d <code>bar<\/code> \u5bf9\u8c61\u662f\u72ec\u7acb\u5b58\u5728\u7684\uff0c\u56e0\u6b64\u597d\u50cf\u4e0d\u9002\u5408\u628a <code>foo<\/code> \u5b9a\u4e49\u4e3a <code>global<\/code>\uff1f<\/p>\n<p>\u5982\u679c\u6682\u4e14\u4e0d\u8003\u8651\u9650\u5236 3\uff0c\u6709\u6ca1\u6709\u4ec0\u4e48\u6bd4\u8f83\u597d\u7684\u65b9\u6cd5\u5b9e\u73b0\uff1f<\/p>\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=\"5399436\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ericls <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             &#8220;`<br \/>class C:<br \/> def __init__(self):<br \/> print(&#8220;here&#8221;)<\/p>\n<p>c_cache = {}<br \/>def make_c():<br \/> import inspect<br \/> frame = inspect.stack()<br \/> key = &#8216;.&#8217;.join([frame[1].filename, frame[1].function])<br \/> if key not in c_cache:<br \/> c_cache[key] = C()<br \/> return c_cache[key]<\/p>\n<p>def foo():<br \/> c = make_c()<\/p>\n<p>def bar():<br \/> c = make_c()<\/p>\n<p>for i in range(100):<br \/> foo()<\/p>\n<p>for i in range(100):<br \/> bar()<br \/>&#8220;`                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5399437\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ericls <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             https:\/\/gist.github.com\/ericls\/3a2a17c7dbceb15462d0fcbe259d2246 <button onclick=\"lazyGist(this)\">\u00a0\u663e\u793a Gist \u4ee3\u7801\u00a0<\/button> <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5399438\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : nightwitch <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             https:\/\/stackoverflow.com\/questions\/279561\/what-is-the-python-equivalent-of-static-variables-inside-a-function<\/p>\n<p>\u53c2\u8003\u6700\u9ad8\u8d5e\u3002Python \u91cc\u9762\u51fd\u6570\u4e5f\u662f Object\uff0c\u6240\u4ee5\u53ef\u4ee5\u7ed9\u51fd\u6570\u6dfb\u52a0\u4e00\u4e2a\u5c5e\u6027                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5399439\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : imn1 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5982\u679c x \u7684\u53d8\u5316\u4e0d\u5927\uff0c\u76f4\u63a5 @lur_cache\uff0c\u90fd\u61d2\u5f97\u8fd0\u884c\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5399440\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hsfzxjy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             C \u7684\u5b9e\u4f8b\u5316\u5355\u72ec\u62ce\u4e00\u4e2a\u51fd\u6570\uff0c\u7136\u540e\u5957\u4e0a lru_cache                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5399441\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : BiteTheDust <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u628a\u51fd\u6570\u5c01\u88c5\u5230\u4e00\u4e2a\u5bf9\u8c61\u91cc                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5399442\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ipwx <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             foo \u53ef\u80fd\u4f1a\u88ab a.py \u8c03\u7528\uff0c\u4e5f\u53ef\u80fd\u88ab b.py \u8c03\u7528\uff0c\u6211\u5e0c\u671b\u5728\u8fd9\u4e24\u4e2a py \u6587\u4ef6\u6267\u884c\u7684\u8fc7\u7a0b\u4e2d bar \u5bf9\u8c61\u662f\u72ec\u7acb\u5b58\u5728\u7684\uff0c\u56e0\u6b64\u597d\u50cf\u4e0d\u9002\u5408\u628a foo \u5b9a\u4e49\u4e3a global \uff1f<br \/>&#8212;-<\/p>\n<p>\u5728 a.py \u548c b.py \u91cc\u9762\u5206\u522b\u5efa\u7acb\u4e00\u4e2a foo \u7684\u4e0d\u540c\u95ed\u5305\u4e0d\u5c31\u884c\u4e86\u3002\u3002\u3002<\/p>\n<p>def make_foo():<br \/>&#8230;.bar = C()<br \/>&#8230;.def foo(x):<br \/>&#8230;&#8230;..return bar(x)<\/p>\n<p>a.py:<\/p>\n<p>foo = make_foo()<\/p>\n<p>b.py:<\/p>\n<p>foo = make_foo()                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5399443\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ipwx <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u987a\u4fbf\u5982\u679c\u662f\u5bf9\u8c61\u7684\u8bdd\u91cd\u8f7d __call__ \u5c31\u80fd\u770b\u4e0a\u53bb\u662f\u51fd\u6570\u4e86\u3002<\/p>\n<p>class Foo(object):<br \/>&#8230;.def __init__(self):<br \/>&#8230;&#8230;..self.bar = C()<br \/>&#8230;.def __call__(self, x):<br \/>&#8230;&#8230;..return self.bar(x)<\/p>\n<p>a. py:<\/p>\n<p>foo = Foo()<\/p>\n<p>b. py:<\/p>\n<p>foo = Foo()                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5399444\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : capbone <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @hsfzxjy \u5982\u679c\u4e0d\u540c\u6a21\u5757\u5206\u522b\u8c03\u7528\uff0clru_cache \u53ea\u4f1a\u7f13\u5b58\u4e00\u4efd\u5427\uff1f\u9650\u5236 3 \u4f3c\u4e4e\u4e0d\u6ee1\u8db3\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5399445\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hsfzxjy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @capbone \u90a3\u4f60\u53ef\u4ee5\u52a0\u4e00\u4e2a\u53c2\u6570\uff0c\u8c03\u7528\u65f6\u628a\u8c03\u7528\u8005\u7684__file__\u4f20\u8fdb\u53bb                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5399446\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : leafcoder <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5ffd\u7565\u7b2c 3 \u6761\u6761\u4ef6\u53ef\u4ee5\u5982\u4e0b\uff1a<\/p>\n<p>class C(object):<br \/> pass<\/p>\n<p>class Demo(object):<\/p>\n<p> def __new__(cls, *args, **kwargs):<br \/> inst = object.__new__(cls, *args, **kwargs)<br \/> if not hasattr(Demo, &#8216;c_inst&#8217;):<br \/> c_inst = C(*args, **kwargs)<br \/> Demo.c_inst = c_inst<br \/> inst.c_inst = Demo.c_inst<br \/> return inst<\/p>\n<p> def __init__(self, *args, **kwargs):<br \/> pass<\/p>\n<p>for i in range(5):<br \/> o = Demo()<br \/> print(o, o.c_inst)<\/p>\n<p>\u8f93\u51fa\uff1a<\/p>\n<p>&gt;&gt;&gt; (&lt;__main__.Demo object at 0x7f7bd9e95310&gt;, &lt;__main__.C object at 0x7f7bd9e95350&gt;)<br \/>&gt;&gt;&gt; (&lt;__main__.Demo object at 0x7f7bd9e95390&gt;, &lt;__main__.C object at 0x7f7bd9e95350&gt;)<br \/>&gt;&gt;&gt; (&lt;__main__.Demo object at 0x7f7bd9e95310&gt;, &lt;__main__.C object at 0x7f7bd9e95350&gt;)<br \/>&gt;&gt;&gt; (&lt;__main__.Demo object at 0x7f7bd9e95390&gt;, &lt;__main__.C object at 0x7f7bd9e95350&gt;)<br \/>&gt;&gt;&gt; (&lt;__main__.Demo object at 0x7f7bd9e95310&gt;, &lt;__main__.C object at 0x7f7bd9e95350&gt;)                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5399447\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : no1xsyzy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @ipwx \u5728 #8 \u57fa\u7840\u4e0a\u53ef\u4ee5\u628a bar \u5199\u6210\u4e00\u4e2a functools.cached_property\uff0c\u53ef\u4ee5\u907f\u514d\u8fc7\u65e9\u6c42\u503c\u3002<\/p>\n<p>@capbone \u9650\u5236 3 \u6309 pep8 \u5e94\u5f53\u66f4\u663e\u5f0f\u5730\u4e66\u5199                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Python \uff1a\u5982\u4f55\u907f\u514d\u67d0\u4e2a\u51fd\u6570\u5185&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\/384600"}],"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=384600"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/384600\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=384600"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=384600"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=384600"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}