{"id":220487,"date":"2020-11-18T14:15:26","date_gmt":"2020-11-18T06:15:26","guid":{"rendered":"http:\/\/4563.org\/?p=220487"},"modified":"2020-11-18T14:15:26","modified_gmt":"2020-11-18T06:15:26","slug":"%e5%85%b3%e4%ba%8e%e7%bc%96%e7%a8%8b%e6%80%9d%e7%bb%b4%e6%96%b9%e5%bc%8f-%e6%83%b3%e8%af%b7%e6%95%99%e4%b8%a4%e4%b8%aa%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=220487","title":{"rendered":"\u5173\u4e8e\u7f16\u7a0b\u601d\u7ef4\u65b9\u5f0f, \u60f3\u8bf7\u6559\u4e24\u4e2a\u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5173\u4e8e\u7f16\u7a0b\u601d\u7ef4\u65b9\u5f0f, \u60f3\u8bf7\u6559\u4e24\u4e2a\u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : plko345 <\/span>  <span><i><\/i> 2<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p><strong>\u95ee\u9898\u4e00<\/strong>: \u6211\u5728\u7c7b\u91cc\u9762\u6709\u6bd4\u8f83\u591a\u7684\u7c7b\u4f3c\u4e0b\u9762\u7684\u5199\u6cd5, \u4f46\u611f\u89c9\u975e\u5e38\u4e0d\u5408\u9002, \u8fd9\u79cd\u60c5\u51b5\u5e94\u8be5\u600e\u4e48\u53bb\u5904\u7406, \u8fd9\u91cc\u4f1a\u591a\u6b21\u7684\u5224\u65ad\u83b7\u53d6\u5c5e\u6027<\/p>\n<pre><code>class Foo(object):     def __init__(self):         self.a = None         self.b = None      def get_a(self):         if not self.a:             self.a = xxxxx      def get_b(self):         if not self.b:             self.b = xxxxx      def do_sth1(self):         self.get_a()        # \u83b7\u53d6 self.a \u5c5e\u6027         # \u4f7f\u7528 self.a      def do_sth2(self):         self.get_a()        # \u83b7\u53d6 self.a \u5c5e\u6027, \u53c8\u4e00\u6b21         self.get_b()        # \u83b7\u53d6 self.b \u5c5e\u6027         # \u4f7f\u7528 self.a \u548c self.b <\/code><\/pre>\n<p><strong>\u95ee\u9898\u4e8c<\/strong>: \u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf, type hint \u6307\u5411\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u7c7b, \u4f46\u8fd9\u4e2a\u53d8\u91cf\u6211\u60f3\u4e3a \u7a7a\u503c(\u7c7b\u4f3c\u4e0b\u9762), \u5e94\u8be5\u600e\u4e48\u5199?<\/p>\n<pre><code>class Bar(object):     pass  v: Bar = None   # \u4f1a\u8b66\u544a\u4e0d\u5e94\u8be5\u8fd9\u4e48\u5199  a_list: list = []   # \u6b63\u786e\u7684\u5199\u6cd5 a_str: str = \"\"    # \u6b63\u786e\u7684\u5199\u6cd5 <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>13<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4443888\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ClericPy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u95ee\u9898 1 \u91cc\u89c1\u8fc7\u521d\u59cb\u5316 self._a \u7136\u540e\u53d6\u7684\u65f6\u5019\u7528 @property \u6765\u53d6, if not self._a: self._a = xxx \u7136\u540e return self._a <br \/>\u6bd4\u8f83\u5e38\u89c1\u4e8e\u9700\u8981\u60f0\u6027\u521d\u59cb\u5316\u534f\u7a0b\u76f8\u5173\u5bf9\u8c61, \u5fc5\u987b\u7b49 running loop \u542f\u52a8\u7684\u65f6\u5019\u4fdd\u8bc1 loop \u4e00\u81f4. \u6216\u8005\u5176\u4ed6\u60f0\u6027\u521d\u59cb\u5316\u7684\u60c5\u51b5<\/p>\n<p>\u95ee\u9898 2 \u7684\u8bdd, \u6211\u76f4\u63a5\u4e0d\u8ba9\u5b83\u63d0\u9192\u4e86<\/p>\n<p>&#8220;python.linting.mypyPath&#8221;: &#8220;mypy&#8221;,<br \/> &#8220;python.linting.mypyArgs&#8221;: [<br \/> &#8220;&#8211;ignore-missing-imports&#8221;,<br \/> &#8220;&#8211;follow-imports=silent&#8221;,<br \/> &#8220;&#8211;show-column-numbers&#8221;,<br \/> &#8220;&#8211;no-strict-optional&#8221;<br \/> ],                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4443889\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Pagliacii <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u95ee\u9898 2 \u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n<p>&#8220;`python<br \/>from typing import Optional<br \/>v: Optional[Bar] = None<br \/>&#8220;`                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4443890\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : plko345 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @ClericPy \u8c22\u8c22\u6307\u70b9, \u8fd8\u6709\u5173\u4e8e\u95ee\u9898\u4e00, \u6211\u662f\u5426\u4ece\u4e00\u5f00\u59cb\u7c7b\u7684\u8bbe\u8ba1\u4e0a\u5c31\u5b58\u5728\u95ee\u9898, \u56e0\u4e3a\u6211\u5e76\u6ca1\u6709\u4f7f\u7528\u5230\u4f60\u8bf4\u7684 \u60f0\u6027\u521d\u59cb\u5316\u534f\u7a0b\u76f8\u5173\u5bf9\u8c61, \u800c\u4e14\u6211\u5199\u4ee3\u7801\u5e38\u5e38\u4f1a\u8fd9\u4e48\u5199, \u6709\u65f6\u8fd8\u4f1a\u76f4\u63a5\u5728 \u6784\u9020\u51fd\u6570\u4e2d `self._a = self._get_a()` , \u6211\u611f\u89c9\u6bd4\u8f83\u9891\u7e41\u8fd9\u4e48\u4f7f\u7528\u662f\u4e0d\u5bf9\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4443891\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : plko345 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Pagliacii \u611f\u8c22, \u8fd9\u79cd\u5199\u6cd5\u50cf rust \u7684 Option                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4443892\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : freakxx <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @plko345 #3 <\/p>\n<p>\u95ee\u9898\u4e00\uff0c<br \/>\u5982\u679c\u53ea\u662f\u4f5c\u4e3a\u5c5e\u6027\uff0c\u5e76\u4e14\u4e0d\u4f1a\u7ecf\u5e38\u6539\u52a8\uff0c\u6bd4\u5982 set a\uff0c\u90a3\u4e48 init \u5c31\u4e22\u8fdb\u53bb\u5c31\u597d\uff0c<br \/>\u6216\u8005\u50cf 1 \u8bf4\u7684\uff0c\u4f60\u6539\u4e3a property \u5199\u6cd5<\/p>\n<p>\u4f60\u8fd9\u4e48\u5199\uff0c\u611f\u89c9\u602a\u7684\u5730\u65b9\u5728\u4e8e\u4f60\u6ca1\u663e\u5f0f\u8fd4\u56de\uff0c\u5e76\u4e14\u5728 get \u8fc7\u7a0b\u8fd8\u505a\u4e86 set \u64cd\u4f5c\uff0c\u663e\u5f97\u4e0d\u591f\u6e05\u723d\u3002<\/p>\n<p>\u95ee\u9898\u4e8c\uff0c<br \/>\u6211\u89c9\u5f97\u8fd9\u4e2a\u5199\u6cd5\u662f\u633a\u602a\u7684\uff0c\u4f60\u4e0d\u9700\u8981\u5728\u8fd9\u91cc\u58f0\u660e\uff0c\u518d\u8d4b\u503c\uff0c<br \/>\u6216\u8005\u8bf4\uff0c\u4f60\u5728\u8fd9\u91cc\u58f0\u660e v\uff0c\u610f\u4e49\u5176\u5b9e\u4e0d\u5927\uff0c\u53cd\u800c\u4f1a\u9020\u6210\u56f0\u6270\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4443893\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Pagliacii <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @plko345 #3 `self.a = self._get_a()` \u8fd9\u6837\u5199\u7684\u8bdd\uff0cself.a \u4e0d\u5c31\u4e3a None \u4e86\u5417\uff1f\u6211\u770b\u4f60\u7684 self._get_a() \u8bbe\u7f6e\u4e86 self.a \u4f46\u662f\u5b83\u6ca1\u6709\u8fd4\u56de\u503c\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u5982\u679c\u4f60\u60f3\u5bf9\u5c5e\u6027\u8d4b\u503c\u6216\u53d6\u503c\u65f6\u4f5c\u51fa\u63a7\u5236\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 property \u8fd9\u4e2a\u88c5\u9970\u5668\uff0c\u5982\u4e0b\uff1a<br \/>![image.png]( https:\/\/i.loli.net\/2020\/12\/04\/FxiKdVtsI9mZJ3R.png)                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4443894\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : plko345 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @freakxx \u591a\u8c22\u5efa\u8bae\uff0c\u95ee\u9898\u4e8c\u6211\u4e3b\u8981\u60f3\u7528\u5728\u51fd\u6570\u53c2\u6570\u91cc\uff0c\u4e0d\u8fc7\u7528\u5230\u7684\u60c5\u51b5\u4e0d\u591a                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4443895\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : plko345 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Pagliacii \u662f\u6211\u6ca1\u8bb2\u6e05\u695a\uff0c`self.a = self._get_a()` \u8fd9\u6837\u5199\u7684\u65f6\u5019\u662f\u6709 return \u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4443896\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zone10 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u524d\u9762\u7684\u5df2\u7ecf\u8bf4\u4e86, @property \u548c optional, \u4f60\u7684 get \u65b9\u6cd5\u90fd\u6ca1\u8fd4\u56de\u503c\u600e\u4e48 get, \u98ce\u683c\u5c31\u4e0d\u5bf9\u4e86, \u4e0d\u592a\u4e86\u89e3\u4f60\u7684\u9700\u6c42, \u53ef\u80fd\u770b\u4e00\u4e0b attrs \u548c pydantic \u8fd9\u4e24\u4e2a\u5e93\u80fd\u6709\u6240\u542f\u53d1                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4443897\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : azcvcza <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8981\u662f JS \u7684\u8bdd,\u6211\u559c\u6b22\u8fd9\u4e48\u505a<br \/>&#8220;`<br \/>class foo{<br \/>constructor(){<br \/> this.data = {};<br \/> this.functions = {};<br \/>}<\/p>\n<p>get(key){<br \/> return this.data[key] ? this.data[key] : undefined;<br \/>}<\/p>\n<p>doSomething(key, data){<br \/> if(this.functions[key]){<br \/> this.functions[key](data);<br \/> } else {<br \/> throw new Error(&#8216;function not exists&#8217;);<br \/> }<br \/>}<br \/>}<\/p>\n<p>var f = new foo();<br \/>f.data = {a : 2};<br \/>f.functions = {a: (data)=&gt;{console.log(data)}}<\/p>\n<p>f.get(a);<br \/>f.doSomething(a,&#8217;hello world&#8217;);<\/p>\n<p>&#8220;`                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4443898\" 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>                                                             functools.cached_property<\/p>\n<p>Backports: &lt;https:\/\/pypi.org\/project\/backports.cached-property\/&gt;                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4443899\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xpresslink <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \uff08\u95ee\u9898\u4e00\uff09\u5b9e\u5728\u7406\u89e3\u4e0d\u4e86\u4f60\u7684\u601d\u7ef4\u3002\u5982\u679c self.a\/b \u7684\u521d\u59cb\u503c\u662f\u56fa\u5b9a\u7684 xxxxx\uff0c\u76f4\u63a5\u653e__init__\u91cc\u9762\u5c31\u53ef\u4ee5\u4e86\u3002<br \/>\u4e3a\u4ec0\u4e48\u8981\u5148\u5728__init__\u8d4b\u4e2a None \u7136\u540e\u518d\u5f04\u4e24\u4e2a\u51fd\u6570\uff0c\u5728\u51fd\u6570\u91cc\u9762\u8fd8\u591a\u6b64\u4e00\u4e3e\u68c0\u67e5\u6709\u6ca1\u6709\u8fd9\u4e2a\u5c5e\u6027\uff0c\u5206\u522b\u518d\u8d4b\u90a3\u4e2a\u5177\u4f53\u7684\u503c\u5462\uff1f\u76f4\u63a5\u5199\u6210\u4e0b\u9762\u8fd9\u6837\u4e0d\u5c31\u884c\u4e86\u4e48\uff1a<br \/>class Foo(object):<br \/>\u25a1\u25a1 def __init__(self):<br \/>\u25a1\u25a1\u25a1\u25a1self.a = xxxxx<br \/>\u25a1\u25a1\u25a1\u25a1self.b = xxxxx<br \/>\u95ee\u9898\u4e8c: <br \/>&gt;&gt;&gt; v:Bar = None<br \/>&gt;&gt;&gt; v<br \/>&gt;&gt;&gt; b:Bar = Bar()<br \/>&gt;&gt;&gt; b<br \/>&lt;__main__.Bar object at 0x000001FC1AD36448&gt;<br \/>None \u8868\u793a\u7a7a\u503c,\u5b83\u662f\u4e00\u4e2a\u7279\u6b8a Python \u5bf9\u8c61, None \u7684\u7c7b\u578b\u662f NoneType <br \/>\u6240\u4ee5\u4f60\u8bf4\u7684\u8fd9\u4e2a\u53d8\u91cf\u6211\u60f3\u4e3a\u7a7a\u503c\u8fd9\u4e2a\u6982\u5ff5\u5e76\u4e0d\u9002\u5408 Python \u8bed\u8a00\u7684\u53d8\u91cf\u3002<\/p>\n<p>\u5728 python \u4e2d\u53d8\u91cf\u5e76\u4e0d\u540c\u4e8e C++\u8fd9\u7c7b\u7684\u9759\u6001\u8bed\u8a00\uff0c\u5e76\u4e0d\u662f\u5bb9\u5668\u7684\u6982\u5ff5\u3002<br \/>\u5728 python \u4e2d\u8d4b\u503c\u8fc7\u7a0b\u662f\u5148\u6267\u884c\u7b49\u53f7\u53f3\u8fb9\u8bed\u53e5\u521b\u5efa\u5bf9\u8c61\uff0c\u7136\u540e\u628a\u5bf9\u8c61\u5730\u5740\u8d4b\u7ed9\u5de6\u8fb9\u7684\u201c\u53d8\u91cf\u201d\uff0c\u672c\u8d28\u4e0a\u53d8\u91cf\u53ea\u662f\u4e2a\u6807\u7b7e\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4443900\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : plko345 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @xpresslink \u95ee\u9898\u4e00\u91cc\uff0c&#8221;xxxxx&#8221; \u662f\u4e00\u8fde\u4e32\u7684\u8fc7\u7a0b\uff0cself.a \u5c5e\u6027\u53ef\u80fd\u7528\u5230\u4e5f\u53ef\u80fd\u7528\u4e0d\u5230\uff0c\u7528\u4e0d\u5230\u5c31\u6ca1\u5fc5\u8981\u53bb\u6267\u884c xxxxx \u7684\u8fc7\u7a0b\uff0c\u6240\u4ee5\u624d\u6709\u4e2a get \u64cd\u4f5c(\u7ecf\u63d0\u9192\u5e94\u8be5\u6539\u6210 set)\uff0c\u4e0d\u8fc7\u6211\u4e5f\u8bf4\u4e86\u6211\u89c9\u5f97\u8fd9\u4e48\u8bbe\u8ba1\u611f\u89c9\u4e0d\u5bf9\u3002\u95ee\u9898\u4e8c\u6211\u89c9\u5f97\u4e8c\u5927\u5927\u7684\u65b9\u5f0f\u6bd4\u8f83\u7b26\u5408\u6211\u7684\u9700\u8981\uff0c\u800c\u4e14 Python \u90fd\u6709 type hint \u4e86\uff0c\u4e5f\u5e94\u8be5\u8981\u8003\u8651\u52a0\u5165\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u65b9\u6848\uff0c\u4e8c\u7684\u5e94\u8be5\u7b97\u662f\u5b98\u65b9\u7684\u4e00\u79cd\u89e3\u51b3\u65b9\u6848\u4e86\u5427                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5173\u4e8e\u7f16\u7a0b\u601d\u7ef4\u65b9\u5f0f, \u60f3\u8bf7\u6559\u4e24\u4e2a\u95ee\u9898&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\/220487"}],"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=220487"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/220487\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=220487"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=220487"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=220487"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}