{"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
\u641e\u61c2Python\u7a0b\u5e8f\u7684\u4f5c\u7528\u57df<\/span>\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> 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> <\/body>\u547d\u540d\u7a7a\u95f4<\/h2>\n
<\/p>\n
\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 <\/p>\n
\u4f5c\u7528\u57df<\/h2>\n
\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 <\/p>\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
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\u51fd\u6570\u5185\u662f\u5c40\u90e8\u53d8\u91cf : 30
\u51fd\u6570\u5916\u662f\u5168\u5c40\u53d8\u91cf : 0<\/span><\/code><\/pre>\n<\/p><\/div>\nglobal\u5173\u952e\u5b57<\/h2>\n
x = 100<\/span>
def<\/span> sum():
global<\/span> x
x = 200<\/span>
sum<\/span>()
print (x)<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n200<\/span><\/code><\/pre>\n<\/p><\/div>\n
nonlocal\u5173\u952e\u5b57<\/h2>\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>\n100
100100<\/span><\/code><\/pre>\n<\/p><\/div>\n
\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}]}}