{"id":205392,"date":"2025-05-29T16:19:40","date_gmt":"2025-05-29T08:19:40","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205392\/"},"modified":"2025-05-29T16:19:40","modified_gmt":"2025-05-29T08:19:40","slug":"python%e4%bb%a3%e7%a0%81%e4%b8%ad%e6%97%b6%e9%97%b4%e4%b8%8d%e6%9b%b4%e6%96%b0%e5%92%8css-%e6%9c%aa%e5%ae%9a%e4%b9%89%e9%97%ae%e9%a2%98%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205392\/","title":{"rendered":"Python\u4ee3\u7801\u4e2d\u65f6\u95f4\u4e0d\u66f4\u65b0\u548c\u201css \u672a\u5b9a\u4e49\u201d\u95ee\u9898\u5982\u4f55\u89e3\u51b3\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python\u4ee3\u7801\u4e2d\u65f6\u95f4\u4e0d\u66f4\u65b0\u548c\u201css \u672a\u5b9a\u4e49\u201d\u95ee\u9898\u5982\u4f55\u89e3\u51b3\uff1f<\/h1>\n<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241130\/1732965882674af5fa3caa2.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>python \u65f6\u95f4\u4e0d\u66f4\u65b0\u548c\u201css \u672a\u5b9a\u4e49\u201d\u95ee\u9898<\/strong><\/p>\n<p>\u5728\u4f60\u7684python\u4ee3\u7801\u4e2d\u9047\u5230\u4e86\u4e24\u4e2a\u95ee\u9898\uff1a\u65f6\u95f4\u4e0d\u66f4\u65b0\u548c\u201css \u672a\u5b9a\u4e49\u201d\u3002<\/p>\n<p><strong>\u65f6\u95f4\u4e0d\u66f4\u65b0<\/strong><\/p>\n<p>\u8fd9\u662f\u56e0\u4e3a\u4f60\u6ca1\u6709\u5728\u5faa\u73af\u4e2d\u91cd\u65b0\u8bbe\u7f6e\u5168\u5c40\u53d8\u91cf t \u548c\u7c7b\u5c5e\u6027 ss\u3002t \u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u88ab\u8d4b\u503c\uff0css \u5728\u7c7b\u7684\u7b2c\u4e00\u6b21\u521d\u59cb\u5316\u65f6\u88ab\u8d4b\u503c\u3002\u5728\u5faa\u73af\u4e2d\uff0c\u4f60\u5fc5\u987b\u91cd\u65b0\u8d4b\u503c\u8fd9\u4e9b\u53d8\u91cf\uff0c\u4ee5\u4fbf\u5b83\u4eec\u53cd\u6620\u5f53\u524d\u65f6\u95f4\u3002<\/p>\n<p>\u6539\u8fdb\u540e\u7684\u4ee3\u7801\uff1a<\/p>\n<pre>import time\n\nt = time.strftime('%h:%m', time.localtime())\n\nclass ee:\n    ss = time.strftime('%h:%m', time.localtime())\n\n    def a(self):\n        print(t, 'a() t')\n\n    def b(self):\n        print(ee.ss, 'b() ee.ss')\n\n    def c(self):\n        global ss\n        print(ss, 'c() global ss')\n\n    def d(self):\n        print(time.strftime('%h:%m', time.localtime()), 'd()')\n\nif __name__ == '__main__':\n    z = ee()\n    while true:\n        t = time.strftime('%h:%m', time.localtime())\n        z.ss = time.strftime('%h:%m', time.localtime())\n        z.a()\n        z.b()\n        z.c()\n        z.d()\n        time.sleep(2)<\/pre>\n<p><strong>\u201css \u672a\u5b9a\u4e49\u201d<\/strong><\/p>\n<p>\u8fd9\u662f\u56e0\u4e3a\u4f60\u5c1d\u8bd5\u5728\u5168\u5c40\u8303\u56f4\u5185\u8bbf\u95ee\u7c7b\u5c5e\u6027 ss\uff0c\u4f46\u6ca1\u6709\u5728\u5168\u5c40\u8303\u56f4\u5185\u5b9a\u4e49\u5b83\u3002\u89e3\u51b3\u65b9\u6cd5\u662f\u4f7f\u7528 global \u5173\u952e\u5b57\u5c06 ss \u58f0\u660e\u4e3a\u5168\u5c40\u53d8\u91cf\uff0c\u6216\u4f7f\u7528\u7c7b\u65b9\u6cd5\u8bbf\u95ee\u5b83\u3002<\/p>\n<p>\u6539\u8fdb\u540e\u7684\u4ee3\u7801\uff1a<\/p>\n<pre>import time\n\nclass ee:\n    def __init__(self):\n        global ss\n        ss = time.strftime('%H:%M', time.localtime())\n\n    def a(self):\n        print(t, 'a() t')\n\n    def b(self):\n        print(ee.ss, 'b() ee.ss')\n\n    def c(self):\n        print(ss, 'c() global ss')\n\n    def d(self):\n        print(time.strftime('%H:%M', time.localtime()), 'd()')\n\nif __name__ == '__main__':\n    ee()  # \u8c03\u7528\u6784\u9020\u51fd\u6570\n    while True:\n        z = ee()\n        z.a()\n        z.b()\n        z.c()\n        z.d()\n        time.sleep(2)<\/pre>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6587\u7ae0\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u4ee3\u7801\u4e2d\u65f6\u95f4\u4e0d\u66f4\u65b0\u548c\u201c&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-205392","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205392","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=205392"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205392\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205392"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205392"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}