{"id":253,"date":"2016-05-10T15:20:26","date_gmt":"2016-05-10T07:20:26","guid":{"rendered":"http:\/\/www.5x44.cn\/?p=253"},"modified":"2019-01-21T14:37:27","modified_gmt":"2019-01-21T06:37:27","slug":"python3-scopes-and-namespace","status":"publish","type":"post","link":"https:\/\/www.5x44.cn\/?p=253","title":{"rendered":"[Python3] \u7c7b"},"content":{"rendered":"<p>\uff081\uff09namespace \u662f\u4eceobject\u5230name\u7684\u6620\u5c04\u3002<br \/>\uff082\uff09namespace\u7528dict\u5b9e\u73b0\uff0c\u901a\u5e38\u8fd9\u79cd\u6620\u5c04\u4e0d\u53ef\u89c1\u3002<br \/>\uff083\uff09\u5185\u5efa\u7a0b\u5e8f\u7684\u6a21\u5757\u88ab\u79f0\u4e3abuinltins<br \/>\uff084\uff09\u7a0b\u5e8f\u6216\u811a\u672c\u7684\u9876\u7ea7\u6a21\u5757\u88ab\u79f0\u4e3a\u201c__main__\u201d<br \/>\uff085\uff09scope\u662fPython\u7684\u6587\u672c\u57df\uff0c\u5b83\u7684namespace\u662fdirectly accessible\u3002\u201cDirectly accessible\u201d\u610f\u5473\u7740\u4e00\u4e2a\u672a\u6388\u6743\u7684\u5f15\u7528\u8bd5\u56fe\u5728namespace\u4e2d\u67e5\u627ename\u3002<br \/>\uff086\uff09\u867d\u7136scope\u662f\u9759\u6001\u7684\uff0c\u4f46\u5b83\u88ab\u7528\u4e8e\u52a8\u6001\u3002\u5728\u4efb\u4f55\u6267\u884c\u671f\u95f4\uff0c\u81f3\u5c11\u6709\u4e09\u4e2a\u5d4c\u5957\u57df\uff0c\u5b83\u4eec\u7684namespace\u662f\u76f4\u63a5\u8bbf\u95ee\u7684\uff1a<br \/>\u203b\u6700\u91cc\u5c42\u7684scope\u662f\u6700\u5148\u88ab\u641c\u7d22\u7684\uff0c\u5b83\u5305\u542b\u4e86\u672c\u5730\u540d\u3002<br \/>\u203b\u4efb\u4f55\u7a0b\u5e8f\u7684scope\uff0c\u5148\u4ece\u6700\u8fd1\u7684\u5c01\u95ed\u57df\uff08enclosing scope\uff09\u5f00\u59cb\u641c\u7d22\uff0c\u5b83\u5305\u542b\u4e86\u975e\u672c\u5730\uff0c\u800c\u4e14\u975e\u5168\u5c40\u7684\u540d\u5b57\u3002<br \/>\u203b\u4ece\u7b2c\u4e8c\u4e2a\u5230\u6700\u540e\u4e00\u4e2ascope\u5305\u542b\u4e86\u5f53\u524d\u6a21\u5757\u7684\u5168\u5c40\u540d\u3002<br \/>\u203b\u6700\u5916\u5c42scope\uff08\u6700\u540e\u641c\u7d22\uff09\u662f\u5305\u542b\u4e86built-in\u540d\u7684namespace\u3002<br \/>\uff087\uff09\u5982\u679c\u540d\u5b57\u58f0\u660e\u6210global\uff0c\u90a3\u4e48\u6240\u6709\u5f15\u7528\u548c\u8d4b\u503c\u76f4\u63a5\u5230\u4e2d\u95f4\u57df\uff08middle scope\uff09,\u5b83\u5305\u542b\u4e86\u6a21\u5757\u7684\u5168\u5c40\u540d\u3002<br \/>\uff088\uff09\u5728\u6700\u5185\u5c42\u7684\u57df\u4e4b\u5916\u91cd\u65b0\u7ed1\u5b9a\u53d8\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528nonlocal\u58f0\u660e\u3002\u5982\u679c\u58f0\u660e\u4e2d\u6ca1\u6709nonlocal\uff0c\u53d8\u91cf\u662f\u53ea\u8bfb\u7684\u3002<br \/>\uff089\uff09\u901a\u5e38\u672c\u5730\u57df\u5f15\u7528\u5f53\u524d\u7a0b\u5e8f\u7684\u672c\u5730\u540d\u3002\u5916\u90e8\u7a0b\u5e8f\u7684\u672c\u5730\u57df\u5f15\u7528\u76f8\u540c\u7684namespace\u4f5c\u4e3a\u5168\u5c40\u57df\u3002\u7c7b\u7684\u5b9a\u4e49\u5728\u672c\u5730\u57df\u4e2d\u653e\u7f6e\u4e86\u53e6\u4e00\u4e2anamespace\u3002<br \/>\uff0810\uff09\u533a\u57df\u662f\u7531\u6587\u672c\u51b3\u5b9a\u7684\uff0c\u8fd9\u70b9\u5f88\u91cd\u8981\uff1a\u4e00\u4e2a\u51fd\u6570\u7684\u5168\u5c40\u57df\u5b9a\u4e49\u5728\u4e00\u4e2a\u6a21\u5757\u4e2d\uff0c\u8be5\u5168\u5c40\u57df\u5c31\u662f\u6a21\u5757\u7684\u540d\u5b57\u7a7a\u95f4\uff0c\u548c\u5728\u54ea\u91cc\uff08\u6216\u4f7f\u7528\u4ec0\u4e48\u522b\u540d\uff09\u8c03\u7528\u51fd\u6570\u65e0\u5173\u3002<br \/>\uff0811\uff09 \u5982\u679c\u6ca1\u6709global\u58f0\u660e\u7684\u5f71\u54cd\uff0c\u7ed1\u5b9a\u5bf9\u8c61\u548c\u540d\u53ea\u662f\u5728\u6700\u8fd1\u7684\u533a\u57df\u3002assignment\u5e76\u4e0d\u590d\u5236\u6570\u636e\u2014\u2014\u53ea\u662f\u628aname\u548cobject\u7ed1\u5b9a\u3002\u5220\u9664\u65f6\u4e5f\u662f\u4e00\u6837\uff1a\u8bed\u53e5 del x\uff0c\u662f\u5c06\u88ab\u672c\u5730\u57df\u5f15\u7528\u7684x\u4ecenamespace\u4e2d\u79fb\u9664\u3002\u5b9e\u9645\u4e0a\uff0c\u6240\u6709\u4ea7\u751f\u65b0\u540d\u5b57\u7684\u64cd\u4f5c\u4f7f\u7528\u7684\u90fd\u662f\u672c\u5730\u57df\uff1aimport\u58f0\u660e\u548c\u51fd\u6570\u5b9a\u4e49\u662f\u5728\u672c\u5730\u57df\u7ed1\u5b9a\u6a21\u5757\u6216\u51fd\u6570\u540d\u3002<br \/>\uff0812\uff09global\u58f0\u660e\u4e86\u7279\u5b9a\u7684\u53d8\u91cf\u5b58\u5728\u4e8e\u5168\u5c40\u57df\uff0cnonlocal\u58f0\u660e\u4e86\u7279\u5b9a\u7684\u53d8\u91cf\u5b58\u5728\u4e8e\u5f53\u524d\u533a\u57df\u7684\u5916\u5c42\uff0c\u73b0\u5728\u5728\u5185\u5c42\u91cd\u65b0\u7ed1\u5b9a\u4e00\u6b21\u3002<br \/><span style=\"font-size: large;\"><b>Scopes and Namespaces\u4f8b\u5b50<\/b><\/span><\/p>\n<pre>def scope_test():\n    def do_local():\n        spam = \"local spam\"\/\/\u672c\u5730\u58f0\u540d\u4e0d\u6539\u53d8\u5916\u5c42\u7684spam\u503c\u3002\n    def do_nonlocal():\n        nonlocal spam\n        spam = \"nonlocal spam\"\/\/nonloacl\u5206\u914d\u6539\u53d8\u4e86\u5916\u5c42\u7684spam\u503c\u3002\n    def do_global():\n        global spam\n        spam = \"global spam\"\/\/global\u5206\u914d\u6539\u53d8\u6210\u6a21\u5757\u7ea7\u522b\u7ed1\u5b9a\uff0c\u76f8\u5f53\u4e8e\u5728\u5168\u5c40\u57df\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u53d8\u91cfspam\uff0c\u6b64\u65f6\u4e0d\u6539\u53d8\u5916\u5c42\u7684spam\u503c\u3002\n<span style=\"color: #ff0000;\">    spam = \"test spam\"\/\/\u5728do_nonlocal()\u51fd\u6570\u4e2d\uff0c\u8fd9\u91cc\u7684spam\u5c31\u662fnonlocal<\/span>\n    do_local()\n    print(\"After local assignment:\", spam)\n    do_nonlocal()\n    print(\"After nonlocal assignment:\", spam)\n    do_global()\n    print(\"After global assignment:\", spam)\n\nscope_test()\nprint(\"In global scope:\", spam)<\/pre>\n<p><b>The output of the example code is:<\/b><\/p>\n<div>\n<div>\n<pre>After local assignment: test spam\nAfter nonlocal assignment: nonlocal spam\nAfter global assignment: nonlocal spam\nIn global scope: global spam\n<\/pre>\n<\/div>\n<p>\uff0813\uff09__doc__\u662f\u6709\u6548\u7c7b\u5c5e\u6027\uff0c\u5b83\u8fd4\u56de\u5c5e\u4e8e\u8be5\u7c7b\u7684docstring\u3002<\/p>\n<\/div>\n<div>\uff0814\uff09__init__()\u65b9\u6cd5\uff0c\u7c7b\u4f3c\u4e8ec++\u4e2d\u7684\u6784\u9020\u51fd\u6570\u3002\u5728\u7c7b\u4e2d\u7528def __init__(self):\u6765\u5b9a\u4e49\uff0cself\u7c7b\u4f3cC++\u4e2d\u7684this\u6307\u9488\u3002<\/div>\n<div>\uff0815\uff09MyClass\u662f\u4e00\u4e2a\u7c7b\uff0cx\u662f\u8be5\u7c7b\u7684\u4e00\u4e2a\u5b9e\u4f8b\uff0cf()\u662f\u8be5\u7c7b\u7684\u4e00\u4e2a\u65b9\u6cd5\u3002x.f\u662f\u65b9\u6cd5\u5bf9\u8c61\uff08method object\uff09\uff1bMyClass.f\u662f\u51fd\u6570\u5bf9\u8c61\uff08function object\uff09\uff1b\u5982\u679c\u4e00\u4e2a\u201d\u540d\u5b57\u201c\u662f\u6709\u6548\u7684\u7c7b\u5c5e\u6027\uff0c\u5e76\u4e14\u662f\u4e2a\u51fd\u6570\u5bf9\u8c61\uff0c\u90a3\u4e48\u65b9\u6cd5\u5bf9\u8c61\u662f\u5f53\u6307\u5411\uff08pointers to\uff09\u5b9e\u4f8b\u5bf9\u8c61\u65f6\u521b\u5efa\u7684\uff0c\u5e76\u4e14\u540c\u65f6\u51fd\u6570\u5bf9\u8c61\u5728\u8be5\u62bd\u8c61\u7c7b\u4e2d\u88ab\u627e\u5230\u3002\u5f53\u4e00\u4e2a\u65b9\u6cd5\u5bf9\u8c61\u4f7f\u7528\u53c2\u6570\u5217\u8868\u8c03\u7528\u65f6\uff0c\u4e00\u4e2a\u65b0\u7684\u53c2\u6570\u5217\u8868\u5728\u5b9e\u4f8b\u5bf9\u8c61\u4e2d\u6784\u5efa\uff0c\u5e76\u4e14\u51fd\u6570\u5bf9\u8c61\u4f7f\u7528\u8fd9\u4e2a\u65b0\u53c2\u6570\u5217\u8868\u8fdb\u884c\u8c03\u7528\u3002<\/div>\n<div>\uff0816\uff09x.f()\u5b8c\u5168\u7b49\u540c\u4e8eMyClass.f(x)\u3002<\/div>\n<div>\uff0817\uff09\u7c7b\u5bf9\u8c61\u652f\u6301\u4e24\u7c7b\u64cd\u4f5c\uff1a\u5c5e\u6027\u7d22\u5f15\u548c\u5b9e\u4f8b\u5316\u3002<\/div>\n<div>\uff0818\uff09\u5b9e\u4f8b\u53d8\u91cf\uff1a\u6bcf\u4e2a\u5b9e\u4f8b\u4e2d\u7684\u6570\u636e\u662f\u552f\u4e00\u7684\uff1b<br \/>\u7c7b\u00a0\u00a0 \u53d8\u91cf\uff1a\u6240\u6709\u7684\u5b9e\u4f8b\u5171\u4eab\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/div>\n<div>\uff0819\uff09\u5728python\u4e2d\u65e0\u6cd5\u50cfC++\u90a3\u6837\u5b8c\u5168\u5730\u9690\u85cf\u7c7b\u7684\u5b9e\u73b0\u7ec6\u8282\uff0c\u5e76\u4e0d\u662f\u6280\u672f\u505a\u4e0d\u5230\uff0c\u800c\u4ec5\u4ec5\u662f\u56e0\u4e3a\u4e60\u60ef\u624d\u8bbe\u8ba1\u6210\u8fd9\u6837\u3002<\/div>\n<div>\uff0820\uff09\u7c7b\u7684\u7ee7\u627f<span style=\"color: #008000;\">class<\/span> <span style=\"color: #3366ff;\">\u6d3e\u751f\u540d<\/span>(\u57fa\u7c7b\u540d):<\/div>\n<div>\uff0821\uff09\u5982\u679c\u88ab\u8bf7\u6c42\u7684\u5c5e\u6027\u6ca1\u6709\u5728\u7c7b\u4e2d\u627e\u5230\uff0c\u67e5\u627e\u4f1a\u5728\u5176\u57fa\u7c7b\u4e2d\u7ee7\u7eed\u3002\u8fd9\u4e2a\u89c4\u5219\u662f\u9012\u5f52\u7684\uff0c\u76f4\u5230\u6ca1\u6709\u57fa\u7c7b\u3002<\/div>\n<div>\uff0822\uff09\u6d3e\u751f\u7c7b\u7684\u65b9\u6cd5\u53ef\u4ee5\u8986\u76d6\u5176\u57fa\u7c7b\u7684\u65b9\u6cd5\u3002\u5f53\u8c03\u7528\u67d0\u4e00\u5bf9\u8c61\u7684\u65b9\u6cd5A()\u65f6\uff0c\u65b9\u6cd5A()\u7684\u57fa\u7c7b\u65b9\u6cd5\u53c8\u8c03\u7528\u540c\u4e00\u7c7b\u4e2d\u7684\u65b9\u6cd5B()\uff0c\u5728\u8fd9\u4e00\u8c03\u7528\u8fc7\u7a0b\u4e2d\uff0c\u5f88\u6709\u53ef\u80fd\u6700\u540e\u8c03\u7528\u7684\u662f\u5728\u8be5\u6d3e\u751f\u7c7b\u4e2d\u8986\u76d6B()\u7684\u65b9\u6cd5\u3002\uff08\u5bf9\u4e8eC++\u7a0b\u5e8f\u5458\u6765\u8bf4\u5728PYTHON\u4e2d\u6240\u6709\u7684\u65b9\u6cd5\u5b9e\u9645\u4e0a\u90fd\u662fvirtual\u7684\uff09<\/div>\n<div>\uff0823\uff09\u5728\u6d3e\u751f\u7c7b\u4e2d\u8c03\u7528\u57fa\u7c7b\u65b9\u6cd5\uff1aBaseClassName.methodname(self, arguments)\u3002<\/div>\n<div>\uff0824\uff09Python\u6709\u4e24\u4e2a\u5185\u5efa\u51fd\u6570\u7528\u4e8e\u7ee7\u627f\uff1a<\/div>\n<ul>\n<li>isinstance()\uff1a\u7528\u4e8e\u68c0\u67e5\u5b9e\u4f8b\u7c7b\u578b\uff1aisinstance(obj,int)\u5982\u679cobj.__class__\u662fint\u6216int\u7684\u6d3e\u751f\u7c7b\uff0c\u90a3\u4e48\u8fd4\u56deTrue\u3002<\/li>\n<li>issubclass()\uff1a\u7528\u4e8e\u68c0\u67e5\u7c7b\u7ee7\u627f\uff1aissubclass(bool,int)\u8fd4\u56deTrue\uff0c\u56e0\u4e3abool\u662fint\u7684\u5b50\u7c7b\u3002\u4f46\u662fissubclass(float,int)\u8fd4\u56defalse\uff0c\u56e0\u4e3afloat\u4e0d\u662fint\u7684\u5b50\u7c7b\u3002<\/li>\n<\/ul>\n<p>\uff0825\uff09\u591a\u7ee7\u627f <span style=\"color: #339966;\">class<\/span> <span style=\"color: #3366ff;\">\u6d3e\u751f\u7c7b\u540d<\/span>(base1, base2,base3):<br \/>\u5982\u679c\u5728\u6d3e\u751f\u7c7b\u4e2d\u6ca1\u6709\u627e\u5230\u8981\u67e5\u627e\u7684\u5c5e\u6027\uff0c\u90a3\u4e48\u4e4b\u540e\u4f1a\u5728base1\u7684\u7ee7\u627f\u7ed3\u6784\u4e2d\u9012\u5f52\u67e5\u627e\uff0c\u5982\u679c\u6ca1\u627e\u5230\u4f1a\u5728base2\u4e2d\u8fdb\u884c\u540c\u6837\u7684\u64cd\u4f5c\u3002<\/p>\n<p>\uff0826\uff09\u5f53\u7c7b\u6210\u5458\u7684\u540d\u5b57\u4ee5\u4e00\u4e2a\u4e0b\u5212\u7ebf\u5f00\u5934\u65f6\uff08\u5305\u62ec\u65b9\u6cd5\u548c\u6570\u636e\u6210\u5458\uff09\uff0c\u4e60\u60ef\u4e0a\u88ab\u770b\u6210\u662f\u975e\u516c\u6709\u7684API\uff08\u4e5f\u5c31\u662f_spam\uff09\u3002<\/p>\n<p>\uff0827\uff09\u5f53\u4e00\u4e2a\u6807\u793a\u7b26\u7684\u5f62\u5f0f\u662f\u201c__spam\u201d\uff08\u81f3\u5c11\u4e24\u4e2a\u4e0b\u5212\u7ebf\u524d\u7f00\uff0c\u81f3\u591a\u4e00\u4e2a\u4e0b\u5212\u7ebf\u540e\u7f00\uff09\uff0c\u5176\u6587\u672c\u4f1a\u88ab\u66ff\u6362\u6210_classname__spam\u7684\u5f62\u5f0f\u3002classname\u662f\u5f53\u524d\u7c7b\u7684\u540d\u5b57\u3002\u8fd9\u79cd\u66ff\u6362\u548c\u6807\u793a\u7b26\u7684\u4f4d\u7f6e\u65e0\u5173\uff0c\u53ea\u8981\u662f\u88ab\u5b9a\u4e49\u5728\u7c7b\u91cc\uff0c\u5c31\u4f1a\u88ab\u66ff\u6362\u3002\u5728\u7c7b\u4e2d\u53ef\u4ee5\u4f7f\u7528__spam\u8fd9\u79cd\u5f62\u5f0f\u8c03\u7528\uff0c\u5728\u7c7b\u5916\u8981\u4f7f\u7528_classname__spam\u8fd9\u79cd\u5f62\u5f0f\u8c03\u7528\u3002<\/p>\n<p>\uff0828\uff09<a href=\"https:\/\/www.5x44.cn\/?p=89\">\u8fed\u4ee3\u5668\u7c7b\u578biterator<\/a><\/p>\n<p>\uff0829\uff09<a href=\"https:\/\/www.5x44.cn\/?p=154\">generators<\/a><\/p>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\uff081\uff09namespace \u662f\u4eceobject\u5230name\u7684\u6620\u5c04\u3002\uff082\uff09namespace\u7528dict\u5b9e\u73b0\uff0c\u901a\u5e38\u8fd9\u79cd\u6620\u5c04\u4e0d\u53ef\u89c1\u3002\uff083\uff09\u5185\u5efa\u7a0b\u5e8f\u7684\u6a21\u5757\u88ab\u79f0\u4e3abuinlti&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.5x44.cn\/?p=253\"> 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":[15],"tags":[],"class_list":["post-253","post","type-post","status-publish","format-standard","hentry","category-python3"],"_links":{"self":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/253","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=253"}],"version-history":[{"count":35,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/253\/revisions"}],"predecessor-version":[{"id":5765,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/253\/revisions\/5765"}],"wp:attachment":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}