{"rsdb":{"rid":"397508","subhead":"","postdate":"0","aid":"273599","fid":"77","uid":"1","topic":"1","content":"
\n

\u8fd9\u4e2a\u7410\u788e\u7684\u6307\u5357\u662f\u5173\u4e8e\u5728 Python \u4e2d\u8de8\u591a\u4e2a\u6587\u4ef6\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u3002\u4f46\u662f\u5728\u8fdb\u5165\u4e3b\u9898\u4e4b\u524d\uff0c\u8ba9\u6211\u4eec\u7b80\u5355\u5730\u770b\u770b\u5168\u5c40\u53d8\u91cf\u548c\u5b83\u4eec\u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\u7684\u7528\u9014\u3002
Python \u4e2d\u7684\u5168\u5c40\u53d8\u91cf<\/p> \n

\u5168\u5c40\u53d8\u91cf\u662f\u4e0d\u5c5e\u4e8e\u51fd\u6570\u8303\u56f4\u7684\u53d8\u91cf\uff0c\u53ef\u4ee5\u5728\u6574\u4e2a\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u3002\u8fd9\u8868\u660e\u5168\u5c40\u53d8\u91cf\u4e5f\u53ef\u4ee5\u5728\u51fd\u6570\u4f53\u5185\u90e8\u6216\u5916\u90e8\u4f7f\u7528\u3002<\/p> \n

\u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p> \n

x = "my global var"\ndef fun1():\n    print("Inside fun1(): ",x)\nfun1()\nprint("In global context: ",x)\n<\/code><\/pre> \n 

\u6211\u4eec\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5168\u5c40\u53d8\u91cfx \uff0c\u5e76\u7ed9\u5b83\u5206\u914d\u4e86\u4e00\u4e9b\u503c\u3002\u7136\u540e\uff0c\u6211\u4eec\u5728\u51fd\u6570\u5185\u90e8\u548c\u5916\u90e8\u6253\u5370\u4e86x \u53d8\u91cf\uff0c\u4ee5\u67e5\u770b\u5728\u4e24\u4e2a\u8303\u56f4\u5185\u7684\u503c\u3002<\/p> \n

\u8f93\u51fa\u7ed3\u679c\uff1a
\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"
\u8f93\u51fa\u8868\u660e\u53d8\u91cf\u7684\u503c\u5728\u51fd\u6570\u5185\u90e8\u548c\u5916\u90e8\u90fd\u662f\u4e00\u6837\u7684\u3002\u5982\u679c\u6211\u4eec\u9700\u8981\u5728\u4e00\u4e9b\u5c40\u90e8\u8303\u56f4\u5185\u6539\u53d8\u5168\u5c40\u53d8\u91cf\u7684\u503c\uff0c\u6bd4\u5982\u5728\u4e00\u4e2a\u51fd\u6570\u4e2d\uff0c\u90a3\u4e48\u6211\u4eec\u9700\u8981\u5728\u58f0\u660e\u53d8\u91cf\u65f6\u4f7f\u7528\u5173\u952e\u5b57global \u3002<\/p> \n

\u8de8\u591a\u4e2a\u6587\u4ef6\u4f7f\u7528\u5168\u5c40\u53d8\u91cf<\/h3> \n

\u5982\u679c\u6211\u4eec\u7684\u7a0b\u5e8f\u4f7f\u7528\u591a\u4e2a\u6587\u4ef6\uff0c\u5e76\u4e14\u8fd9\u4e9b\u6587\u4ef6\u9700\u8981\u66f4\u65b0\u53d8\u91cf\uff0c\u90a3\u4e48\u6211\u4eec\u5e94\u8be5\u50cf\u8fd9\u6837\u7528global \u5173\u952e\u5b57\u6765\u58f0\u660e\u53d8\u91cf\uff1a<\/p> \n

global x = "My global var"\n<\/code><\/pre> \n 

\u8003\u8651\u4e00\u4e2a\u4f8b\u5b50\uff0c\u6211\u4eec\u5fc5\u987b\u5904\u7406\u591a\u4e2aPython\u4ee3\u7801\u6587\u4ef6\u548c\u4e00\u4e2a\u5b66\u751f\u540d\u5355\u7684\u5168\u5c40\u53d8\u91cf\u3002resource.py \u6587\u4ef6\u6709\u4e00\u4e2a\u5b66\u751f\u7684\u5168\u5c40\u5217\u8868\uff0cprog.py \u6709\u4e00\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5b66\u751f\u8ffd\u52a0\u5230\u8fd9\u4e2a\u5168\u5c40\u5217\u8868\u4e2d\u3002<\/p> \n

\u6211\u4eec\u53ef\u4ee5\u7528\u4e0b\u9762\u7684\u4ee3\u7801\u6765\u5b9e\u73b0\u8fd9\u4e2a\u6982\u5ff5\uff1a<\/p> \n

\u4ee3\u7801-resource.py \uff1a<\/p> \n

def initialize():\n    global students\n    students = []\n<\/code><\/pre> \n 

\u4ee3\u7801-prog.py \uff1a<\/p> \n

import resource\ndef addStudent():\n    resource.students.append('John')\n    resource.students.append('Dave')\n<\/code><\/pre> \n 

\u4ee3\u7801-Main.py \uff1a<\/p> \n

#Python\u5c0f\u767d\u5b66\u4e60\u4ea4\u6d41\u7fa4\uff1a711312441\nimport resource\nimport prog\nresource.initialize()\nprog.addStudent()\nprint(resource.students[0])\nprint(resource.students[1])\n<\/code><\/pre> \n 

\u5728\u7b2c\u4e00\u4e2aresource.py \u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570\uff0c\u5176\u4e2d\u6211\u4eec\u58f0\u660e\u4e86\u4e00\u4e2a\u5217\u8868studentList \uff0c\u5e76\u5c06\u5176\u521d\u59cb\u5316\u4e3a\u4e00\u4e2a\u7a7a\u5217\u8868\u3002\u5728\u4e0b\u4e00\u4e2a\u6587\u4ef6\uff08\u5373prog.py \uff09\u4e2d\uff0c\u6211\u4eec\u5305\u542b\u4e86resource \u6a21\u5757\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570addStudent \uff0c\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\u6211\u4eec\u5728\u5168\u5c40\u5217\u8868studentList \u4e2d\u8ffd\u52a0\u4e86\u4e24\u4e2a\u5bf9\u8c61\u3002<\/p> \n

\u5728\u4e3b\u6587\u4ef6Main.py \u4e2d\uff0c\u6211\u4eec\u5305\u542b\u4e86\u4e24\u4e2a\u6a21\u5757\uff0cresource \u548cprog \u3002\u540e\u6765\uff0c\u6211\u4eec\u5206\u522b\u8c03\u7528\u4e86\u8fd9\u4e24\u4e2a\u6a21\u5757\u7684\u51fd\u6570initialize \u548caddStudent \u3002<\/p> \n

\u4e4b\u540e\uff0c\u5f53\u6211\u4eec\u6253\u5370\u5217\u8868\u7d22\u5f15\u65f6\uff0c\u6211\u4eec\u5f97\u5230\u4e86\u4ee5\u4e0b\u8f93\u51fa\uff1a
\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"
\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528global \u5173\u952e\u5b57\u6765\u5b9a\u4e49\u4e00\u4e2a Python \u6587\u4ef6\u4e2d\u7684\u5168\u5c40\u53d8\u91cf\uff0c\u4ee5\u4fbf\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u4f7f\u7528\u3002\u73b0\u5728\uff0c\u8981\u5728\u53e6\u4e00\u4e2a\u6587\u4ef6\u4e2d\u8bbf\u95ee\u4e00\u4e2a\u6587\u4ef6\u7684\u5168\u5c40\u53d8\u91cf\uff0c\u628a\u6709\u5168\u5c40\u53d8\u91cf\u7684\u6587\u4ef6\u4f5c\u4e3a\u53e6\u4e00\u4e2a\u6587\u4ef6\u7684\u6a21\u5757\u5bfc\u5165\uff0c\u76f4\u63a5\u8bbf\u95ee\u5bfc\u5165\u6a21\u5757\u7684\u4efb\u4f55\u5168\u5c40\u53d8\u91cf\uff0c\u800c\u4e0d\u9700\u8981\u989d\u5916\u7684\u590d\u6742\u64cd\u4f5c\u3002<\/p> \n<\/div>","orderid":"0","title":"Python\u4e2d\u8de8\u8d8a\u591a\u4e2a\u6587\u4ef6\u4f7f\u7528\u5168\u5c40\u53d8\u91cf","smalltitle":"","mid":"0","fname":"Python","special_id":"0","bak_id":"0","info":"0","hits":"518","pages":"1","comments":"0","posttime":"2023-09-23 15:44:25","list":"1695455065","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"Python<\/A> \u8d8a\u591a\u4e2a<\/A> \u6587\u4ef6\u4f7f<\/A> \u5168\u5c40\u53d8<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"112.94.1.100","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"Python\u4e2d\u8de8\u8d8a\u591a\u4e2a\u6587\u4ef6\u4f7f\u7528\u5168\u5c40\u53d8\u91cf","lastview":"1713310026","digg_num":"883","digg_time":"1712734471","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}