{"id":7210,"date":"2026-09-14T16:56:42","date_gmt":"2026-09-14T08:56:42","guid":{"rendered":"https:\/\/t.n-years.com\/?p=7210"},"modified":"2026-09-14T16:56:42","modified_gmt":"2026-09-14T08:56:42","slug":"python-%e8%af%ad%e6%b3%95%e9%80%9f%e9%80%9a","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=7210","title":{"rendered":"Python \u8bed\u6cd5\u901f\u901a"},"content":{"rendered":"<p><strong>Python \u8bed\u6cd5\u901f\u901a<\/strong><\/p>\n<pre><code class=\"language-python\"># \u6ce8\u91ca\n# \u5355\u884c\u6ce8\u91ca\n&quot;&quot;&quot;\u591a\u884c\u5b57\u7b26\u4e32 \/ \u6587\u6863\u6ce8\u91ca&quot;&quot;&quot;<\/code><\/pre>\n<pre><code class=\"language-python\"># \u53d8\u91cf\u4e0e\u57fa\u7840\u7c7b\u578b\nname = &quot;Alice&quot;         # str\nage = 18               # int\nprice = 9.99           # float\nenabled = True         # bool\nnothing = None         # \u7a7a\u503c\n\n# \u67e5\u770b\u7c7b\u578b\u3001\u8f6c\u6362\ntype(age)              # &lt;class &#039;int&#039;&gt;\nint(&quot;12&quot;)              # 12\nstr(12)                # &quot;12&quot;\nlist(&quot;abc&quot;)            # [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]<\/code><\/pre>\n<pre><code class=\"language-python\"># \u5bb9\u5668\nnumbers = [1, 2, 3]                    # list\uff1a\u53ef\u4fee\u6539\u3001\u6709\u5e8f\npoint = (10, 20)                       # tuple\uff1a\u4e0d\u53ef\u4fee\u6539\u3001\u6709\u5e8f\ntags = {&quot;python&quot;, &quot;django&quot;}             # set\uff1a\u53bb\u91cd\u3001\u65e0\u5e8f\nuser = {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 18}    # dict\uff1a\u952e\u503c\u5bf9\n\nnumbers.append(4)\nnumbers[0]          # 1\nnumbers[-1]         # 4\nnumbers[1:3]        # [2, 3]\n\nuser[&quot;name&quot;]        # &quot;Alice&quot;\nuser.get(&quot;email&quot;)   # None\uff0c\u4e0d\u4f1a\u56e0\u952e\u4e0d\u5b58\u5728\u62a5\u9519\nuser[&quot;city&quot;] = &quot;Shanghai&quot;<\/code><\/pre>\n<pre><code class=\"language-python\"># \u6761\u4ef6\u5224\u65ad\uff1a\u7f29\u8fdb\u4ee3\u8868\u4ee3\u7801\u5757\nif age &gt;= 18:\n    print(&quot;adult&quot;)\nelif age &gt;= 12:\n    print(&quot;teen&quot;)\nelse:\n    print(&quot;child&quot;)\n\n# \u903b\u8f91\u8fd0\u7b97\nif enabled and age &gt; 0:\n    pass\n\nif not enabled:\n    pass<\/code><\/pre>\n<pre><code class=\"language-python\"># \u5faa\u73af\nfor number in numbers:\n    print(number)\n\nfor index, value in enumerate(numbers):\n    print(index, value)\n\nfor key, value in user.items():\n    print(key, value)\n\nfor index in range(3):       # 0, 1, 2\n    print(index)\n\nwhile age &lt; 20:\n    age += 1\n\n# continue \u8df3\u8fc7\u672c\u8f6e\uff1bbreak \u7ed3\u675f\u5faa\u73af<\/code><\/pre>\n<pre><code class=\"language-python\"># \u5217\u8868 \/ \u5b57\u5178\u63a8\u5bfc\u5f0f\nsquares = [number ** 2 for number in range(10)]\nevens = [number for number in range(10) if number % 2 == 0]\nname_lengths = {name: len(name) for name in [&quot;Tom&quot;, &quot;Alice&quot;]}<\/code><\/pre>\n<pre><code class=\"language-python\"># \u51fd\u6570\ndef add(left, right=0):\n    &quot;&quot;&quot;\u8fd4\u56de\u4e24\u4e2a\u6570\u4e4b\u548c\u3002&quot;&quot;&quot;\n    return left + right\n\nresult = add(1, 2)\nresult = add(right=2, left=1)\n\n# \u4efb\u610f\u4f4d\u7f6e\u53c2\u6570\u3001\u5173\u952e\u5b57\u53c2\u6570\ndef show(*args, **kwargs):\n    print(args)     # tuple\n    print(kwargs)   # dict<\/code><\/pre>\n<pre><code class=\"language-python\"># \u5f02\u5e38\u5904\u7406\ntry:\n    value = int(&quot;abc&quot;)\nexcept ValueError as error:\n    print(f&quot;\u8f6c\u6362\u5931\u8d25\uff1a{error}&quot;)\nelse:\n    print(&quot;\u672a\u53d1\u751f\u5f02\u5e38&quot;)\nfinally:\n    print(&quot;\u59cb\u7ec8\u6267\u884c&quot;)<\/code><\/pre>\n<pre><code class=\"language-python\"># \u6587\u4ef6\u64cd\u4f5c\uff1awith \u81ea\u52a8\u5173\u95ed\u6587\u4ef6\nwith open(&quot;data.txt&quot;, &quot;r&quot;, encoding=&quot;utf-8&quot;) as file:\n    content = file.read()\n\nwith open(&quot;data.txt&quot;, &quot;w&quot;, encoding=&quot;utf-8&quot;) as file:\n    file.write(&quot;hello\\n&quot;)<\/code><\/pre>\n<p><strong>\u7c7b\u4e0e\u5bf9\u8c61<\/strong><\/p>\n<pre><code class=\"language-python\">class User:\n    # \u7c7b\u5c5e\u6027\uff1a\u6240\u6709\u5b9e\u4f8b\u5171\u4eab\uff08\u901a\u5e38\u7528\u4e8e\u5e38\u91cf\uff09\n    role = &quot;member&quot;\n\n    # \u6784\u9020\u65b9\u6cd5\uff1a\u521b\u5efa\u5b9e\u4f8b\u65f6\u8c03\u7528\uff1b\u4e0d\u662f\u5fc5\u987b\n    def __init__(self, name, age=0):\n        self.name = name       # \u5b9e\u4f8b\u5c5e\u6027\n        self.age = age\n\n    # \u5b9e\u4f8b\u65b9\u6cd5\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f self\n    def greet(self):\n        return f&quot;Hi, I&#039;m {self.name}&quot;\n\n    # \u7c7b\u65b9\u6cd5\uff0c\u53ef\u8bbf\u95ee\u7c7b\u672c\u8eab\n    @classmethod\n    def guest(cls):\n        return cls(&quot;Guest&quot;)\n\n    # \u9759\u6001\u65b9\u6cd5\uff0c\u4e0e\u7c7b\/\u5b9e\u4f8b\u72b6\u6001\u90fd\u65e0\u5173\n    @staticmethod\n    def is_adult(age):\n        return age &gt;= 18\n\nuser = User(&quot;Alice&quot;, 18)\nuser.greet()<\/code><\/pre>\n<pre><code class=\"language-python\"># \u7ee7\u627f\u4e0e super()\nclass Admin(User):\n    def __init__(self, name, age, permissions):\n        super().__init__(name, age)  # \u8c03\u7528\u7236\u7c7b\u6784\u9020\u65b9\u6cd5\n        self.permissions = permissions\n\n    def greet(self):\n        return f&quot;Admin: {super().greet()}&quot;<\/code><\/pre>\n<p><strong>\u5e38\u89c1 Python \u7279\u6027<\/strong><\/p>\n<pre><code class=\"language-python\"># \u89e3\u5305\nfirst, second = [1, 2]\nfirst, *middle, last = [1, 2, 3, 4]\n\n# f-string\uff1a\u63a8\u8350\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\nmessage = f&quot;{name} is {age} years old&quot;\n\n# \u4e09\u5143\u8868\u8fbe\u5f0f\nstatus = &quot;adult&quot; if age &gt;= 18 else &quot;minor&quot;\n\n# \u7a7a\u503c\u9ed8\u8ba4\u5904\u7406\ndisplay_name = name or &quot;Anonymous&quot;\n\n# \u6210\u5458\u5224\u65ad\n&quot;python&quot; in tags\n&quot;name&quot; in user\n\n# \u76f8\u7b49\u4e0e\u8eab\u4efd\n1 == 1          # \u503c\u76f8\u7b49\nvalue is None   # \u5224\u65ad None \u65f6\u7528 is<\/code><\/pre>\n<p><strong>\u6a21\u5757\u4e0e\u5305<\/strong><\/p>\n<pre><code class=\"language-python\">import os\nfrom datetime import datetime\nfrom pathlib import Path as FilePath\n\nprint(datetime.now())<\/code><\/pre>\n<p>\u8fd0\u884c\u5165\u53e3\uff1a<\/p>\n<pre><code class=\"language-python\">def main():\n    print(&quot;start&quot;)\n\nif __name__ == &quot;__main__&quot;:\n    main()<\/code><\/pre>\n<p><strong>DRF \u5173\u8054\u91cd\u70b9<\/strong><\/p>\n<pre><code class=\"language-python\">class MyViewSet(ReadOnlyViewSet):\n    queryset = Model.objects.all()       # \u7c7b\u5c5e\u6027\n    serializer_class = MySerializer      # \u7c7b\u5c5e\u6027\n\n    def list(self, request, *args, **kwargs):\n        response = super().list(request, *args, **kwargs)\n        return response<\/code><\/pre>\n<ul>\n<li><code>def<\/code> \u5b9a\u4e49\u51fd\u6570\u6216\u65b9\u6cd5\u3002<\/li>\n<li><code>self<\/code> \u4ee3\u8868\u5f53\u524d\u5b9e\u4f8b\u3002<\/li>\n<li><code>__init__<\/code> \u4e0d\u662f\u5fc5\u5199\uff1b\u4ec5\u5728\u9700\u8981\u521d\u59cb\u5316\u5b9e\u4f8b\u72b6\u6001\u65f6\u5b9a\u4e49\u3002<\/li>\n<li>\u7c7b\u65b9\u6cd5\u4e2d\u8c03\u7528\u7236\u7c7b\u5b9e\u73b0\uff0c\u73b0\u4ee3 Python \u63a8\u8350 <code>super()<\/code>\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Python \u8bed\u6cd5\u901f\u901a # \u6ce8\u91ca # \u5355\u884c\u6ce8\u91ca &quot;&quot;&quot;\u591a\u884c\u5b57\u7b26\u4e32 \/ \u6587\u6863\u6ce8\u91ca&#038;&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=7210\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-7210","post","type-post","status-publish","format-standard","hentry","category-5"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/7210","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7210"}],"version-history":[{"count":1,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/7210\/revisions"}],"predecessor-version":[{"id":7211,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/7210\/revisions\/7211"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}