Transaction: ded693aa3a7ef0775e1c9e7996a6f25c349244c2

Included in block 22,247,950 at 2018/05/08 10:41:06 (UTC).

Transaction overview

Loading...
Transaction info
transaction_id ded693aa3a7ef0775e1c9e7996a6f25c349244c2
ref_block_num 31,227
block_num22,247,950
ref_block_prefix 466,202,624
expiration2018/05/08T10:51:03
transaction_num 56
extensions[]
signatures 203d4330e85a8997b29e88646bf56ea4c5f460ce0ff817e0fbd83357274646e5691f0f935fca982bfe4e243e5727d493b164b71262e05e66e1020ebf035f26bbab
operations
comment
"parent_author":"",<br>"parent_permlink":"python",<br>"author":"tengyue",<br>"permlink":"python-global-nonlocal",<br>"title":"python\u4e2dglobal \u548c nonlocal \u7684\u4f5c\u7528\u57df",<br>"body":"\u6700\u8fd1\u5728\u5b66\u4e60python\uff0c\u5728\u8fd9\u91cc\u8bb0\u5f55\u5b66\u4e60\u4e2d\u7684\u95ee\u9898\n\n### global\n> global\u5173\u952e\u5b57\u7528\u6765\u5728\u51fd\u6570\u6216\u5176\u4ed6\u5c40\u90e8\u4f5c\u7528\u57df\u4e2d\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u3002**`\u4f46\u662f\u5982\u679c\u4e0d\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u4e5f\u53ef\u4ee5\u4e0d\u4f7f\u7528global\u5173\u952e\u5b57\u3002`**\n```\nIn [5 : gcount = 0\n\nIn [6 : def global_test():\n ...: gcount+=1\n ...: print (gcount)\n ...: \n\nIn [7 : global_test()\n---------------------------------------------------------------------------\nUnboundLocalError Traceback (most recent call last)\n<ipython-input-7-86f3510ae66b> in <module>()\n----> 1 global_test()\n\n<ipython-input-6-ef97fb6917fb> in global_test()\n 1 def global_test():\n----> 2 gcount+=1\n 3 print (gcount)\n 4 \n\nUnboundLocalError: local variable 'gcount' referenced before assignment\n```\n\n\u7b2c\u4e00\u884c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\uff0c\uff08\u53ef\u4ee5\u7701\u7565global\u5173\u952e\u5b57\uff09\u3002\n\u5728global_test \u51fd\u6570\u4e2d\u7a0b\u5e8f\u4f1a\u56e0\u4e3a\u201c\u5982\u679c\u5185\u90e8\u51fd\u6570\u6709\u5f15\u7528\u5916\u90e8\u51fd\u6570\u7684\u540c\u540d\u53d8\u91cf\u6216\u8005\u5168\u5c40\u53d8\u91cf,<br>\u5e76\u4e14\u5bf9\u8fd9\u4e2a\u53d8\u91cf\u6709\u4fee\u6539.\u90a3\u4e48python\u4f1a\u8ba4\u4e3a\u5b83\u662f\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf,<br>\u53c8\u56e0\u4e3a\u51fd\u6570\u4e2d\u6ca1\u6709gcount\u7684\u5b9a\u4e49\u548c\u8d4b\u503c\uff0c\u6240\u4ee5\u62a5\u9519\u3002\n\n### \u58f0\u660e\u5168\u5c40\u53d8\u91cf\u3002\n> \u5982\u679c\u5728\u5c40\u90e8\u8981\u5bf9\u5168\u5c40\u53d8\u91cf\u4fee\u6539\uff0c\u9700\u8981\u5728\u5c40\u90e8\u4e5f\u8981\u5148\u58f0\u660e\u8be5\u5168\u5c40\u53d8\u91cf\n```\nIn [8 : gcount = 0\n\nIn [9 : def global_test():\n ...: global gcount\n ...: gcount+=1\n ...: print (gcount)\n ...: \n\nIn [10 : global_test()\n1\n```\n\u5982\u679c\u5728\u51fd\u6570\u4e2d\u58f0\u660e gcount \u662f\u5168\u5c40\u53d8\u91cf\uff0c\u5373\u53ef\u5bf9\u5176\u8fdb\u884c\u4fee\u6539\u3002 \u6b63\u786e\u8f93\u51fa 1 \u3002\n\n### \u5982\u679c\u4e0d\u4fee\u6539\u5168\u5c40\u53d8\u91cf\n> \u5728\u5c40\u90e8\u5982\u679c\u4e0d\u58f0\u660e\u5168\u5c40\u53d8\u91cf\uff0c\u5e76\u4e14\u4e0d\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u3002\u5219\u53ef\u4ee5\u6b63\u5e38\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\n```\nIn [11 : gcount = 0\n\nIn [12 : def global_test():\n ...: print (gcount)\n ...: \n ...: \n\nIn [13 : global_test()\n0\n```\n\u5982\u679c\u5728\u5c40\u90e8\u4e0d\u4fee\u6539\u5168\u5c40\u53d8\u91cf\uff0c\u7a0b\u5e8f\u6b63\u786e\u8f93\u51fa 0 \u3002\n\n### nonlocal\u5173\u952e\u5b57\n> \u7528\u6765\u5728\u51fd\u6570\u6216\u5176\u4ed6\u4f5c\u7528\u57df\u4e2d\u4f7f\u7528\u5916\u5c42(\u975e\u5168\u5c40)\u53d8\u91cf\u3002\n```\nIn [14 : def make_counter():\n ...: count = 0\n ...: def counter():\n ...: nonlocal count\n ...: count += 1\n ...: return count\n ...: return counter\n ...: \n\nIn [15 : def make_counter_test():\n ...: mc = make_counter()\n ...: print(mc())\n ...: print(mc())\n ...: print(mc())\n ...: \n\n\nIn [16 : make_counter_test()\n1\n2\n3\n```\n\n### \u6d4b\u8bd5\n```\nIn [35 : def scope_test():\n ...: spam = \"test spam\"\n ...: def do_local():\n ...: spam = 'local spam'\n ...: def do_nonlocal():\n ...: nonlocal spam\n ...: spam = 'nonlocal spam'\n ...: def do_global():\n ...: global spam\n ...: spam = 'global spam'\n ...: do_local()\n ...: print('after local assignmane:',<br> spam)\n ...: do_nonlocal()\n ...: print('after nonlocal assignment:',<br> spam)\n ...: do_global()\n ...: print('after global assignment:',<br> spam)\n ...: \n\nIn [36 : scope_test()\nafter local assignmane: test spam\nafter nonlocal assignment: nonlocal spam\nafter global assignment: nonlocal spam\n\nIn [37 : print('in global scope:',<br> spam)\nin global scope: global spam\n```",<br>"json_metadata":" \"tags\":[\"python\" ,<br>\"app\":\"steemit\/0.1\",<br>\"format\":\"markdown\" "
* The API used to generate this page is provided by @steemchiller.