{"id":1032,"date":"2023-03-24T09:47:07","date_gmt":"2023-03-24T01:47:07","guid":{"rendered":""},"modified":"2023-03-24T09:47:07","modified_gmt":"2023-03-24T01:47:07","slug":"Python \u4f5c\u7528\u57df","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1032.html","title":{"rendered":"Python \u4f5c\u7528\u57df"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

Python \u4f5c\u7528\u57df<\/h1>\n

\u641e\u61c2Python\u7a0b\u5e8f\u7684\u4f5c\u7528\u57df<\/span>\n <\/div>\n

\u547d\u540d\u7a7a\u95f4<\/h2>\n
\n \u547d\u540d\u7a7a\u95f4(Namespace)\u662f\u4ece\u540d\u79f0\u5230\u5bf9\u8c61\u7684\u6620\u5c04\uff0c\u5927\u90e8\u5206\u7684\u547d\u540d\u7a7a\u95f4\u90fd\u662f\u901a\u8fc7 Python \u5b57\u5178\u6765\u5b9e\u73b0\u7684\u3002\n <\/div>\n
\n \u547d\u540d\u7a7a\u95f4\u63d0\u4f9b\u4e86\u5728\u9879\u76ee\u4e2d\u907f\u514d\u540d\u5b57\u51b2\u7a81\u7684\u4e00\u79cd\u65b9\u6cd5\u3002\u5404\u4e2a\u547d\u540d\u7a7a\u95f4\u662f\u72ec\u7acb\u7684\uff0c\u6ca1\u6709\u4efb\u4f55\u5173\u7cfb\u7684\uff0c\u6240\u4ee5\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\u4e0d\u80fd\u6709\u91cd\u540d\uff0c\u4f46\u4e0d\u540c\u7684\u547d\u540d\u7a7a\u95f4\u662f\u53ef\u4ee5\u91cd\u540d\u800c\u6ca1\u6709\u4efb\u4f55\u5f71\u54cd\u3002\n <\/div>\n
\n \u6211\u4eec\u4e3e\u4e00\u4e2a\u8ba1\u7b97\u673a\u7cfb\u7edf\u4e2d\u7684\u4f8b\u5b50\uff0c\u4e00\u4e2a\u6587\u4ef6\u5939(\u76ee\u5f55)\u4e2d\u53ef\u4ee5\u5305\u542b\u591a\u4e2a\u6587\u4ef6\u5939\uff0c\u6bcf\u4e2a\u6587\u4ef6\u5939\u4e2d\u4e0d\u80fd\u6709\u76f8\u540c\u7684\u6587\u4ef6\u540d\uff0c\u4f46\u4e0d\u540c\u6587\u4ef6\u5939\u4e2d\u7684\u6587\u4ef6\u53ef\u4ee5\u91cd\u540d\u3002\n <\/div>\n

Python \u4f5c\u7528\u57df_https:\/\/bianchenghao6.com_\u3010Python3 \u6559\u7a0b\u3011_\u7b2c1\u5f20 <\/p>\n

\n \u4e00\u822c\u6709\u4e09\u79cd\u547d\u540d\u7a7a\u95f4\uff1a\n <\/div>\n

\u5185\u7f6e\u540d\u79f0\uff08built-in names\uff09\uff0c Python \u8bed\u8a00\u5185\u7f6e\u7684\u540d\u79f0\uff0c\u6bd4\u5982\u51fd\u6570\u540d abs\u3001char \u548c\u5f02\u5e38\u540d\u79f0 BaseException\u3001Exception \u7b49\u7b49\u3002<\/span>
\n \u5168\u5c40\u540d\u79f0\uff08global names\uff09\uff0c\u6a21\u5757\u4e2d\u5b9a\u4e49\u7684\u540d\u79f0\uff0c\u8bb0\u5f55\u4e86\u6a21\u5757\u7684\u53d8\u91cf\uff0c\u5305\u62ec\u51fd\u6570\u3001\u7c7b\u3001\u5176\u5b83\u5bfc\u5165\u7684\u6a21\u5757\u3001\u6a21\u5757\u7ea7\u7684\u53d8\u91cf\u548c\u5e38\u91cf\u3002<\/span>
\n \u5c40\u90e8\u540d\u79f0\uff08local names\uff09\uff0c\u51fd\u6570\u4e2d\u5b9a\u4e49\u7684\u540d\u79f0\uff0c\u8bb0\u5f55\u4e86\u51fd\u6570\u7684\u53d8\u91cf\uff0c\u5305\u62ec\u51fd\u6570\u7684\u53c2\u6570\u548c\u5c40\u90e8\u5b9a\u4e49\u7684\u53d8\u91cf\u3002\uff08\u7c7b\u4e2d\u5b9a\u4e49\u7684\u4e5f\u662f\uff09<\/span>
\n Python \u4f5c\u7528\u57df_https:\/\/bianchenghao6.com_\u3010Python3 \u6559\u7a0b\u3011_\u7b2c2\u5f20 <\/p>\n

\n \u547d\u540d\u7a7a\u95f4\u67e5\u627e\u987a\u5e8f\u4e3a\uff1a\u5c40\u90e8\u7684\u547d\u540d\u7a7a\u95f4\u53bb -> \u5168\u5c40\u547d\u540d\u7a7a\u95f4 -> \u5185\u7f6e\u547d\u540d\u7a7a\u95f4\u3002\n <\/div>\n
\n \u547d\u540d\u7a7a\u95f4\u7684\u751f\u547d\u5468\u671f\u53d6\u51b3\u4e8e\u5bf9\u8c61\u7684\u4f5c\u7528\u57df\uff0c\u5982\u679c\u5bf9\u8c61\u6267\u884c\u5b8c\u6210\uff0c\u5219\u8be5\u547d\u540d\u7a7a\u95f4\u7684\u751f\u547d\u5468\u671f\u5c31\u7ed3\u675f\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u65e0\u6cd5\u4ece\u5916\u90e8\u547d\u540d\u7a7a\u95f4\u8bbf\u95ee\u5185\u90e8\u547d\u540d\u7a7a\u95f4\u7684\u5bf9\u8c61\u3002\n <\/div>\n

\u4f5c\u7528\u57df<\/h2>\n
\n \u4f5c\u7528\u57df\u5c31\u662f\u4e00\u4e2a Python \u7a0b\u5e8f\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u547d\u540d\u7a7a\u95f4\u7684\u6b63\u6587\u533a\u57df\u3002\n <\/div>\n
\n \u5728\u4e00\u4e2a python \u7a0b\u5e8f\u4e2d\uff0c\u76f4\u63a5\u8bbf\u95ee\u4e00\u4e2a\u53d8\u91cf\uff0c\u4f1a\u4ece\u5185\u5230\u5916\u4f9d\u6b21\u8bbf\u95ee\u6240\u6709\u7684\u4f5c\u7528\u57df\u76f4\u5230\u627e\u5230\uff0c\u5426\u5219\u4f1a\u62a5\u672a\u5b9a\u4e49\u7684\u9519\u8bef\u3002\n <\/div>\n
\n Python \u4e2d\uff0c\u7a0b\u5e8f\u7684\u53d8\u91cf\u5e76\u4e0d\u662f\u5728\u54ea\u4e2a\u4f4d\u7f6e\u90fd\u53ef\u4ee5\u8bbf\u95ee\u7684\uff0c\u8bbf\u95ee\u6743\u9650\u51b3\u5b9a\u4e8e\u8fd9\u4e2a\u53d8\u91cf\u662f\u5728\u54ea\u91cc\u8d4b\u503c\u7684\u3002\n <\/div>\n
\n \u53d8\u91cf\u7684\u4f5c\u7528\u57df\u51b3\u5b9a\u4e86\u5728\u54ea\u4e00\u90e8\u5206\u7a0b\u5e8f\u53ef\u4ee5\u8bbf\u95ee\u54ea\u4e2a\u7279\u5b9a\u7684\u53d8\u91cf\u540d\u79f0\u3002Python\u7684\u4f5c\u7528\u57df\u4e00\u5171\u67094\u79cd\uff0c\u5206\u522b\u662f\uff1a\n <\/div>\n
\n \u6709\u56db\u79cd\u4f5c\u7528\u57df\uff1a\n <\/div>\n

L\uff08Local\uff09\uff1a\u6700\u5185\u5c42\uff0c\u5305\u542b\u5c40\u90e8\u53d8\u91cf\uff0c\u6bd4\u5982\u4e00\u4e2a\u51fd\u6570\/\u65b9\u6cd5\u5185\u90e8\u3002<\/span>
\n E\uff08Enclosing\uff09\uff1a\u5305\u542b\u4e86\u975e\u5c40\u90e8(non-local)\u4e5f\u975e\u5168\u5c40(non-global)\u7684\u53d8\u91cf\u3002\u6bd4\u5982\u4e24\u4e2a\u5d4c\u5957\u51fd\u6570\uff0c\u4e00\u4e2a\u51fd\u6570\uff08\u6216\u7c7b\uff09 A \u91cc\u9762\u53c8\u5305\u542b\u4e86\u4e00\u4e2a\u51fd\u6570 B \uff0c\u90a3\u4e48\u5bf9\u4e8e B \u4e2d\u7684\u540d\u79f0\u6765\u8bf4 A \u4e2d\u7684\u4f5c\u7528\u57df\u5c31\u4e3a nonlocal\u3002<\/span>
\n G\uff08Global\uff09\uff1a\u5f53\u524d\u811a\u672c\u7684\u6700\u5916\u5c42\uff0c\u6bd4\u5982\u5f53\u524d\u6a21\u5757\u7684\u5168\u5c40\u53d8\u91cf\u3002<\/span>
\n B\uff08Built-in\uff09\uff1a \u5305\u542b\u4e86\u5185\u5efa\u7684\u53d8\u91cf\/\u5173\u952e\u5b57\u7b49\u3002\uff0c\u6700\u540e\u88ab\u641c\u7d22<\/span> <\/p>\n

\n \u89c4\u5219\u987a\u5e8f\uff1a L \u2013> E \u2013> G \u2013>gt; B\u3002\n <\/div>\n
\n \u5728\u5c40\u90e8\u627e\u4e0d\u5230\uff0c\u4fbf\u4f1a\u53bb\u5c40\u90e8\u5916\u7684\u5c40\u90e8\u627e\uff08\u4f8b\u5982\u95ed\u5305\uff09\uff0c\u518d\u627e\u4e0d\u5230\u5c31\u4f1a\u53bb\u5168\u5c40\u627e\uff0c\u518d\u8005\u53bb\u5185\u7f6e\u4e2d\u627e\u3002\n <\/div>\n

Python \u4f5c\u7528\u57df_https:\/\/bianchenghao6.com_\u3010Python3 \u6559\u7a0b\u3011_\u7b2c3\u5f20 <\/p>\n

\n
 a_name = 1<\/span>  # \u5168\u5c40\u4f5c\u7528\u57df <\/span>
def<\/span> outer():
    b_name = 2<\/span> # \u95ed\u5305\u51fd\u6570\u5916\u7684\u51fd\u6570\u4e2d<\/span>
    def<\/span> inner():
        c_name = 3<\/span> # \u5c40\u90e8\u4f5c\u7528\u57df<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n

\u5168\u5c40\u53d8\u91cf\u548c\u5c40\u90e8\u53d8\u91cf<\/h2>\n
\n \u5b9a\u4e49\u5728\u51fd\u6570\u5185\u90e8\u7684\u53d8\u91cf\u62e5\u6709\u4e00\u4e2a\u5c40\u90e8\u4f5c\u7528\u57df\uff0c\u5b9a\u4e49\u5728\u51fd\u6570\u5916\u7684\u62e5\u6709\u5168\u5c40\u4f5c\u7528\u57df\u3002\n <\/div>\n
\n \u5c40\u90e8\u53d8\u91cf\u53ea\u80fd\u5728\u5176\u88ab\u58f0\u660e\u7684\u51fd\u6570\u5185\u90e8\u8bbf\u95ee\uff0c\u800c\u5168\u5c40\u53d8\u91cf\u53ef\u4ee5\u5728\u6574\u4e2a\u7a0b\u5e8f\u8303\u56f4\u5185\u8bbf\u95ee\u3002\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u6240\u6709\u5728\u51fd\u6570\u5185\u58f0\u660e\u7684\u53d8\u91cf\u540d\u79f0\u90fd\u5c06\u88ab\u52a0\u5165\u5230\u4f5c\u7528\u57df\u4e2d\u3002\u5982\u4e0b\u5b9e\u4f8b\uff1a\n <\/div>\n
\n
 total<\/span> = 0<\/span>
def<\/span> sum( arg1, arg2 ):
    total = arg1 + arg2<\/span> # total\u5728\u8fd9\u91cc\u662f\u5c40\u90e8\u53d8\u91cf.<\/span>
    print(\"\u51fd\u6570\u5185\u662f\u5c40\u90e8\u53d8\u91cf : \", total<\/span>)
    return<\/span> total

sum<\/span>( 10, 20 )
print (\"\u51fd\u6570\u5916\u662f\u5168\u5c40\u53d8\u91cf : \", total)<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
\u51fd\u6570\u5185\u662f\u5c40\u90e8\u53d8\u91cf : 30
\u51fd\u6570\u5916\u662f\u5168\u5c40\u53d8\u91cf : 0<\/span><\/code><\/pre>\n<\/p><\/div>\n

global\u5173\u952e\u5b57<\/h2>\n
\n \u5f53\u5185\u90e8\u4f5c\u7528\u57df\u60f3\u4fee\u6539\u5916\u90e8\u4f5c\u7528\u57df\u7684\u53d8\u91cf\u65f6\uff0c\u5c31\u8981\u7528\u5230global\u5173\u952e\u5b57\u4e86\u3002\u5982\u679c\u4f7f\u7528 global \u5173\u952e\u5b57\uff0c\u5219\u8be5\u53d8\u91cf\u5c5e\u4e8e\u5168\u5c40\u8303\u56f4\n <\/div>\n
\n
 x = 100<\/span>
def<\/span> sum():
    global<\/span> x
    x = 200<\/span>
sum<\/span>()
print (x)<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
200<\/span><\/code><\/pre>\n<\/p><\/div>\n

nonlocal\u5173\u952e\u5b57<\/h2>\n
\n \u5982\u679c\u8981\u4fee\u6539\u5d4c\u5957\u4f5c\u7528\u57df\uff08enclosing \u4f5c\u7528\u57df\uff0c\u5916\u5c42\u975e\u5168\u5c40\u4f5c\u7528\u57df\uff09\u4e2d\u7684\u53d8\u91cf\u5219\u9700\u8981 nonlocal \u5173\u952e\u5b57\u4e86\uff0c\u5982\u4e0b\u5b9e\u4f8b\uff1a\n <\/div>\n
\n
 def<\/span> outer():
    num = 10<\/span>
    def<\/span> inner():
        nonlocal<\/span> num # nonlocal\u5173\u952e\u5b57\u58f0\u660e<\/span>
        num = 100<\/span>
        print (num)<\/span>

    inner()<\/span>
    print (num)<\/span>
outer()<\/span>
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
100
100100<\/span><\/code><\/pre>\n<\/p><\/div>\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Python \u4f5c\u7528\u57dfzh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[126],"tags":[],"class_list":["post-1032","post","type-post","status-publish","format-standard","hentry","category-python3"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1032"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=1032"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1032\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1032"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1032"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1032"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}