{"id":85,"date":"2016-04-04T20:46:57","date_gmt":"2016-04-04T12:46:57","guid":{"rendered":"http:\/\/www.5x44.cn\/?p=85"},"modified":"2017-07-28T10:38:48","modified_gmt":"2017-07-28T02:38:48","slug":"%e7%ac%ac6%e7%ab%a0-closures-generators","status":"publish","type":"post","link":"https:\/\/www.5x44.cn\/?p=85","title":{"rendered":"\u7b2c6\u7ae0 CLOSURES &#038; GENERATORS"},"content":{"rendered":"<p>\uff081\uff09Closures\u2014\u2014\u5c01\u95ed<\/p>\n<p><code>import re<\/code><br \/>\n<code> def build_match_and_apply_functions(pattern, search, replace):\u2460<\/code><br \/>\n<code>\u00a0\u00a0 def matches_rule(word):<\/code><br \/>\n<code>\u00a0\u00a0\u00a0\u00a0 return re.search(pattern, word)<\/code><br \/>\n<code>\u00a0\u00a0 def apply_rule(word): \u2461<\/code><br \/>\n<code>\u00a0\u00a0\u00a0\u00a0 return re.sub(search, replace, word)<\/code><br \/>\n<code>\u00a0\u00a0 return (matches_rule, apply_rule) \u2462<\/code><\/p>\n<ol>\n<li>build_match_and_apply_functions\u53ef\u4ee5\u52a8\u6001\u7684\u5efa\u7acb\u51fd\u6570\uff0c\u5b83\u6709\u4e09\u4e2a\u53c2\u6570pattern, search, replace\u3002<\/li>\n<li>word\u53c2\u6570\u662f\u4e00\u4e2a\u5916\u90e8\u53d8\u91cf\uff0c\u8fd9\u79cd\u5728\u52a8\u6001\u51fd\u6570\u4e2d\u4f7f\u7528\u5916\u90e8\u53c2\u6570\u53d8\u91cf\u7684\u6280\u672f\u53eb\u505a\u201c<em>closures<\/em>\u201d<\/li>\n<li>build_match_and_apply_functions\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4\uff0c\u5143\u7ec4\u4e2d\u6709\u4e24\u4e2a\u51fd\u6570\uff0c\u5206\u522b\u662fmatches_rule\u548capply_rule\uff0c\u8fd9\u4e24\u4e2a\u51fd\u6570\u4e2d\u4f7f\u7528\u7684pattern, search\u548creplace\u53c2\u6570\u90fd\u6765\u81eabuild_match_and_apply_functions\u51fd\u6570\u7684\u53c2\u6570\u5217\u8868\uff0c\u800cword\u6765\u81ea\u5916\u90e8\uff0c\u4e5f\u5c31\u662f\u6700\u7ec8\u4f7f\u7528\u7684\u65f6\u5019\u4f20\u9012\u3002<\/li>\n<\/ol>\n<p><span style=\"color: #99cc00;\">\u521d\u59cb\u5316pattern, search\u548creplace:<\/span><\/p>\n<p><code>patterns = \\ \u2460<\/code><br \/>\n<code> (<\/code><br \/>\n<code> ('[sxz]$','$','es'),<\/code><code>('[^aeioudgkprt]h$','$','es'),<\/code><code>('(qu|[^aeiou])y$','y$','ies'),<\/code><br \/>\n<code> ('$','$','s') \u2461<\/code><br \/>\n<code> )<\/code><br \/>\n<code> rules = [build_match_and_apply_functions(pattern, search, replace)<\/code><br \/>\n<code> for (pattern, search, replace) in patterns]\u2462<\/code><\/p>\n<ol>\n<li>\u521d\u59cb\u5316patterns<\/li>\n<li>\u672a\u5339\u914d\u5230\u7684\u5355\u8bcd\u9ed8\u8ba4\u884c\u4e3a\u52a0s<\/li>\n<li>\u8fd9\u662f\u4e00\u4e2alist comprehension\uff0c\u9996\u5148 (pattern, search, replace)\u4ecepatterns\u4e2d\u5f97\u5230\u503c\uff0c\u7136\u540e\u901a\u8fc7build_match_and_apply_functions(pattern, search, replace)\u8fd4\u56de\u7684\u5143\u7ec4\u5efa\u7acblist\u3002<\/li>\n<\/ol>\n<p><span style=\"color: #99cc00;\">\u6700\u540e\u4f7f\u7528\u7684\u65f6\u5019\uff1a<\/span><\/p>\n<p><code>def plural(noun): \u2460<\/code><br \/>\n<code>\u00a0\u00a0 for <span style=\"color: #339966;\">matches_rule<\/span>,<span style=\"color: #ff6600;\"> apply_rule<\/span> in<span style=\"color: #ff00ff;\"> rules<\/span>:<\/code><br \/>\n<code>\u00a0\u00a0\u00a0\u00a0 if <span style=\"color: #339966;\">matches_rule<\/span>(noun):<\/code><br \/>\n<code>\u00a0\u00a0 return <span style=\"color: #ff6600;\">apply_rule<\/span>(noun)<\/code><\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u5355\u8bcd\u5206\u6790\u7a0b\u5e8f\uff0c\u628a\u540d\u8bcd\u5355\u6570\u5f62\u5f0f\u53d8\u6210\u590d\u6570\u5f62\u5f0f\u3002<\/p>\n<p>\uff082\uff09Generators\u2014\u2014\u751f\u6210\u5668<\/p>\n<p><code>def make_counter(x):<\/code><br \/>\n<code>\u00a0\u00a0 print('entering make_counter')<\/code><br \/>\n<code>\u00a0\u00a0 while True:<\/code><br \/>\n<code>\u00a0\u00a0\u00a0\u00a0 <span style=\"color: #99cc00;\">yield x \u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u2460<\/span><\/code><br \/>\n<code>\u00a0\u00a0\u00a0\u00a0 print('incrementing x')<\/code><br \/>\n<code>\u00a0\u00a0\u00a0\u00a0 x = x + 1<\/code><br \/>\n&#8230;<br \/>\n<span style=\"color: #99cc00;\">counter = make_counter(2)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u2461<\/span><br \/>\n<span style=\"color: #99cc00;\">counter\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u2462<\/span><br \/>\n&lt;generator object at 0x001C9C10&gt;<br \/>\n<span style=\"color: #99cc00;\">next(counter)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u2463<\/span><br \/>\nentering make_counter<br \/>\n2<br \/>\n<span style=\"color: #99cc00;\">next(counter)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u2464<\/span><br \/>\nincrementing x<br \/>\n3<br \/>\n<span style=\"color: #000000;\">next(counter)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/span><br \/>\nincrementing x<br \/>\n4<\/p>\n<ol>\n<li>yield\u6307\u660e\u8fd9\u662f\u4e00\u4e2a\u5f88\u7279\u522b\u7684\u7a0b\u5e8f\uff0cyield\u53ef\u4ee5\u8bb0\u4f4f\u67d0\u4e9b\u503c\u3002<\/li>\n<li>\u5efa\u7acb\u4e00\u4e2a\u751f\u6210\u5668\u5b9e\u4f8b<\/li>\n<li>counter\u5c31\u662fmake_counter()\u7684\u5b9e\u4f8b<\/li>\n<li>next()\u51fd\u6570\u4f7f\u751f\u6210\u5668\u8fd4\u56de\u4e0b\u4e00\u4e2a\u503c\u3002\u6bcf\u6b21\u6267\u884c\u90fd\u4f1a\u6267\u884c\u5230yield\u8bed\u53e5\uff0c\u7136\u540eyield\u4f1a\u8fd4\u56de\u5f53\u524d\u503c\u3002\u672c\u4f8b\u7b2c\u4e00\u6b21\u6267\u884c\u4f1a\u8fd4\u56de2\u3002<\/li>\n<li>\u518d\u6b21\u6267\u884c\u65f6\uff0c\u4f9d\u7136\u5982\u6b64\uff0c\u987a\u5e8f\u5411\u4e0b\u6267\u884c\u4ee3\u7801\uff0c\u548c\u5176\u5b83\u7a0b\u5e8f\u6bb5\u6ca1\u6709\u533a\u522b\u3002<\/li>\n<li>yield\u6682\u505c\u4e00\u4e2a\u7a0b\u5e8f\u6bb5\uff0cnext()\u6062\u590d\u4e0a\u4e00\u6b21\u6682\u505c\u4f4d\u7f6e\u7684\u7a0b\u5e8f\u6bb5\uff0c\u5982\u679c\u51fd\u6570\u5df2\u7ecf\u5230\u4e86\u7ed3\u5c3e\uff0c\u7ee7\u7eed\u4f7f\u7528next()\u4f1a\u5f15\u53d1\u4e00\u4e2aStopIterator\u7684\u5f02\u5e38\u3002<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\uff081\uff09Closures\u2014\u2014\u5c01\u95ed import re def build_match_and_apply_functions(pattern, search, r&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.5x44.cn\/?p=85\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-85","post","type-post","status-publish","format-standard","hentry","category-dive-into-python3"],"_links":{"self":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/85","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=85"}],"version-history":[{"count":10,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/85\/revisions"}],"predecessor-version":[{"id":822,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/85\/revisions\/822"}],"wp:attachment":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=85"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=85"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=85"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}