{"id":10021226,"date":"2026-02-01T02:39:40","date_gmt":"2026-01-31T18:39:40","guid":{"rendered":"https:\/\/itmax.cn\/?p=10021226"},"modified":"2026-02-01T02:39:40","modified_gmt":"2026-01-31T18:39:40","slug":"python%e4%b8%ad%e7%9a%84%e7%b1%bb%e5%9e%8b%e7%b3%bb%e7%bb%9f%e4%b8%8e%e7%b1%bb%e5%9e%8b%e6%b3%a8%e8%a7%a3%e8%bf%9b%e9%98%b6","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10021226","title":{"rendered":"Python\u4e2d\u7684\u7c7b\u578b\u7cfb\u7edf\u4e0e\u7c7b\u578b\u6ce8\u89e3\u8fdb\u9636"},"content":{"rendered":"<h2>Python\u4e2d\u7684\u7c7b\u578b\u7cfb\u7edf\u4e0e\u7c7b\u578b\u6ce8\u89e3\u8fdb\u9636<\/h2>\n<h3>1. \u7c7b\u578b\u7cfb\u7edf\u6982\u8ff0<\/h3>\n<h4>1.1 \u52a8\u6001\u7c7b\u578b\u4e0e\u9759\u6001\u7c7b\u578b<\/h4>\n<p>Python\u4f5c\u4e3a\u4e00\u79cd\u52a8\u6001\u7c7b\u578b\u8bed\u8a00\uff0c\u5176\u53d8\u91cf\u7c7b\u578b\u5728\u8fd0\u884c\u65f6\u786e\u5b9a\uff0c\u8fd9\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u6781\u5927\u7684\u7075\u6d3b\u6027\u3002\u7136\u800c\uff0c\u968f\u7740\u9879\u76ee\u89c4\u6a21\u7684\u6269\u5927\uff0c\u52a8\u6001\u7c7b\u578b\u5e26\u6765\u7684\u7c7b\u578b\u9519\u8bef\u98ce\u9669\u4e5f\u968f\u4e4b\u589e\u52a0\u3002\u7c7b\u578b\u6ce8\u89e3\uff08Type Hints\uff09\u7684\u5f15\u5165\uff0c\u4e3aPython\u63d0\u4f9b\u4e86\u9759\u6001\u7c7b\u578b\u68c0\u67e5\u7684\u80fd\u529b\uff0c\u5e73\u8861\u4e86\u7075\u6d3b\u6027\u4e0e\u4ee3\u7801\u53ef\u9760\u6027\u3002<\/p>\n<h4>1.2 \u7c7b\u578b\u6ce8\u89e3\u7684\u6f14\u8fdb<\/h4>\n<ul>\n<li>Python 3.5\uff1a\u5f15\u5165\u57fa\u672c\u7c7b\u578b\u6ce8\u89e3\u8bed\u6cd5<\/li>\n<li>Python 3.6\uff1a\u652f\u6301\u53d8\u91cf\u6ce8\u89e3<\/li>\n<li>Python 3.7\uff1a\u652f\u6301<code>from __future__ import annotations<\/code>\u5ef6\u8fdf\u6ce8\u89e3\u8bc4\u4f30<\/li>\n<li>Python 3.8\uff1a\u5f15\u5165\u5b57\u9762\u91cf\u7c7b\u578b\u4e0e<code>Final<\/code>\u7c7b\u578b<\/li>\n<li>Python 3.9\uff1a\u5185\u7f6e\u6cdb\u578b\u7c7b\u578b\u652f\u6301<\/li>\n<li>Python 3.10\uff1a\u5f15\u5165\u8054\u5408\u7c7b\u578b\u8bed\u6cd5<code>X | Y<\/code>\u548c\u7c7b\u578b\u522b\u540d<\/li>\n<\/ul>\n<h3>2. \u57fa\u672c\u7c7b\u578b\u6ce8\u89e3<\/h3>\n<h4>2.1 \u51fd\u6570\u53c2\u6570\u4e0e\u8fd4\u56de\u503c\u6ce8\u89e3<\/h4>\n<pre><code class=\"python\">def add(a: int, b: int) -&gt; int:\n    return a + b\n\ndef greet(name: str) -&gt; str:\n    return f&quot;Hello, {name}!&quot;<\/code><\/pre>\n<h4>2.2 \u53d8\u91cf\u6ce8\u89e3<\/h4>\n<pre><code class=\"python\">age: int = 25\nname: str = &quot;Alice&quot;\nis_student: bool = True<\/code><\/pre>\n<h4>2.3 \u590d\u5408\u7c7b\u578b\u6ce8\u89e3<\/h4>\n<pre><code class=\"python\">from typing import List, Dict, Tuple\n\nnumbers: List[int] = [1, 2, 3]\nperson: Dict[str, str] = {&quot;name&quot;: &quot;Bob&quot;, &quot;age&quot;: &quot;30&quot;}\ncoordinates: Tuple[float, float] = (1.0, 2.0)<\/code><\/pre>\n<h3>3. \u9ad8\u7ea7\u7c7b\u578b\u6ce8\u89e3\u6280\u5de7<\/h3>\n<h4>3.1 \u6cdb\u578b\u7c7b\u578b<\/h4>\n<p>\u6cdb\u578b\u5141\u8bb8\u6211\u4eec\u5b9a\u4e49\u9002\u7528\u4e8e\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u548c\u7c7b\uff1a<\/p>\n<pre><code class=\"python\">from typing import TypeVar, Generic, List\n\nT = TypeVar('T')\n\nclass Stack(Generic[T]):\n    def __init__(self):\n        self.items: List[T] = []\n    \n    def push(self, item: T) -&gt; None:\n        self.items.append(item)\n    \n    def pop(self) -&gt; T:\n        return self.items.pop()\n\n# \u4f7f\u7528\u6cdb\u578b\u6808\nint_stack: Stack[int] = Stack()\nstr_stack: Stack[str] = Stack()<\/code><\/pre>\n<h4>3.2 \u8054\u5408\u7c7b\u578b\u4e0e\u53ef\u9009\u7c7b\u578b<\/h4>\n<p>\u8054\u5408\u7c7b\u578b\u8868\u793a\u53d8\u91cf\u53ef\u4ee5\u662f\u591a\u79cd\u7c7b\u578b\u4e4b\u4e00\uff1a<\/p>\n<pre><code class=\"python\">from typing import Union, Optional\n\n# \u8054\u5408\u7c7b\u578b\nvalue: Union[int, str, float] = 42\n\n# \u53ef\u9009\u7c7b\u578b\uff08\u7b49\u540c\u4e8eUnion[T, None]\uff09\ndef get_user(id: int) -&gt; Optional[Dict[str, str]]:\n    # \u53ef\u80fd\u8fd4\u56de\u7528\u6237\u4fe1\u606f\u6216None\n    pass\n\n# Python 3.10+ \u8054\u5408\u7c7b\u578b\u8bed\u6cd5\nvalue: int | str | float = &quot;hello&quot;\noptional_value: str | None = None<\/code><\/pre>\n<h4>3.3 \u5b57\u9762\u91cf\u7c7b\u578b\u4e0e\u5e38\u91cf\u7c7b\u578b<\/h4>\n<p>\u5b57\u9762\u91cf\u7c7b\u578b\u9650\u5236\u53d8\u91cf\u53ea\u80fd\u53d6\u7279\u5b9a\u7684\u503c\uff1a<\/p>\n<pre><code class=\"python\">from typing import Literal, Final\n\n# \u5b57\u9762\u91cf\u7c7b\u578b\ndef set_mode(mode: Literal[&quot;read&quot;, &quot;write&quot;, &quot;append&quot;]) -&gt; None:\n    pass\n\n# \u5e38\u91cf\u7c7b\u578b\nMAX_SIZE: Final[int] = 100<\/code><\/pre>\n<h4>3.4 \u53ef\u8c03\u7528\u7c7b\u578b\u4e0e\u7c7b\u578b\u522b\u540d<\/h4>\n<pre><code class=\"python\">from typing import Callable, TypeAlias\n\n# \u53ef\u8c03\u7528\u7c7b\u578b\nCallback: TypeAlias = Callable[[int, str], bool]\n\ndef process_data(data: List[int], callback: Callback) -&gt; None:\n    pass\n\n# \u7c7b\u578b\u522b\u540d\nUserId: TypeAlias = int\nUserDict: TypeAlias = Dict[str, Union[str, int, bool]]<\/code><\/pre>\n<h3>4. \u7c7b\u578b\u68c0\u67e5\u5de5\u5177<\/h3>\n<h4>4.1 mypy<\/h4>\n<p>mypy\u662fPython\u6700\u6d41\u884c\u7684\u9759\u6001\u7c7b\u578b\u68c0\u67e5\u5de5\u5177\uff1a<\/p>\n<pre><code class=\"bash\"># \u5b89\u88c5\npip install mypy\n\n# \u68c0\u67e5\u5355\u4e2a\u6587\u4ef6\nmypy example.py\n\n# \u68c0\u67e5\u6574\u4e2a\u9879\u76ee\nmypy .<\/code><\/pre>\n<h4>4.2 pyright\u4e0epylance<\/h4>\n<ul>\n<li><strong>pyright<\/strong>\uff1aMicrosoft\u5f00\u53d1\u7684\u5feb\u901f\u9759\u6001\u7c7b\u578b\u68c0\u67e5\u5668<\/li>\n<li><strong>pylance<\/strong>\uff1aVS Code\u7684Python\u8bed\u8a00\u670d\u52a1\u5668\uff0c\u96c6\u6210\u4e86pyright<\/li>\n<\/ul>\n<h4>4.3 \u914d\u7f6e\u6587\u4ef6<\/h4>\n<p>\u521b\u5efa<code>pyproject.toml<\/code>\u6216<code>mypy.ini<\/code>\u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n<pre><code class=\"toml\"># pyproject.toml\n[tool.mypy]\npython_version = &quot;3.10&quot;\nstrict = true\nwarn_return_any = true\nwarn_unused_configs = true<\/code><\/pre>\n<h3>5. \u7c7b\u578b\u6ce8\u89e3\u7684\u6700\u4f73\u5b9e\u8df5<\/h3>\n<h4>5.1 \u4f55\u65f6\u4f7f\u7528\u7c7b\u578b\u6ce8\u89e3<\/h4>\n<ul>\n<li><strong>\u516c\u5171API<\/strong>\uff1a\u4e3a\u6a21\u5757\u3001\u51fd\u6570\u548c\u7c7b\u7684\u516c\u5171\u63a5\u53e3\u6dfb\u52a0\u7c7b\u578b\u6ce8\u89e3<\/li>\n<li><strong>\u590d\u6742\u903b\u8f91<\/strong>\uff1a\u4e3a\u5305\u542b\u590d\u6742\u7c7b\u578b\u8f6c\u6362\u7684\u4ee3\u7801\u6dfb\u52a0\u6ce8\u89e3<\/li>\n<li><strong>\u5927\u578b\u9879\u76ee<\/strong>\uff1a\u5728\u5927\u578b\u4ee3\u7801\u5e93\u4e2d\u5168\u9762\u4f7f\u7528\u7c7b\u578b\u6ce8\u89e3<\/li>\n<li><strong>\u56e2\u961f\u534f\u4f5c<\/strong>\uff1a\u63d0\u9ad8\u4ee3\u7801\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027<\/li>\n<\/ul>\n<h4>5.2 \u907f\u514d\u8fc7\u5ea6\u6ce8\u89e3<\/h4>\n<ul>\n<li>\u7b80\u5355\u7684\u5c40\u90e8\u53d8\u91cf\u53ef\u4ee5\u7701\u7565\u7c7b\u578b\u6ce8\u89e3<\/li>\n<li>\u660e\u663e\u7684\u7c7b\u578b\u53ef\u4ee5\u7701\u7565\u6ce8\u89e3<\/li>\n<li>\u4f7f\u7528\u7c7b\u578b\u63a8\u65ad\u51cf\u5c11\u5197\u4f59\u6ce8\u89e3<\/li>\n<\/ul>\n<h4>5.3 \u7c7b\u578b\u6ce8\u89e3\u4e0e\u6587\u6863\u5b57\u7b26\u4e32<\/h4>\n<p>\u7ed3\u5408\u7c7b\u578b\u6ce8\u89e3\u548c\u6587\u6863\u5b57\u7b26\u4e32\uff0c\u63d0\u4f9b\u66f4\u5168\u9762\u7684\u4ee3\u7801\u6587\u6863\uff1a<\/p>\n<pre><code class=\"python\">def calculate_area(radius: float) -&gt; float:\n    &quot;&quot;&quot;\n    \u8ba1\u7b97\u5706\u7684\u9762\u79ef\n    \n    Args:\n        radius: \u5706\u7684\u534a\u5f84\n    \n    Returns:\n        \u5706\u7684\u9762\u79ef\n    &quot;&quot;&quot;\n    return 3.14159 * radius ** 2<\/code><\/pre>\n<h3>6. \u5b9e\u9645\u5e94\u7528\u6848\u4f8b<\/h3>\n<h4>6.1 \u6570\u636e\u9a8c\u8bc1<\/h4>\n<p>\u4f7f\u7528\u7c7b\u578b\u6ce8\u89e3\u7ed3\u5408\u7b2c\u4e09\u65b9\u5e93\u8fdb\u884c\u6570\u636e\u9a8c\u8bc1\uff1a<\/p>\n<pre><code class=\"python\">from pydantic import BaseModel\n\nclass User(BaseModel):\n    id: int\n    name: str\n    email: str\n    age: int\n\n# \u81ea\u52a8\u9a8c\u8bc1\nuser = User(id=1, name=&quot;Alice&quot;, email=&quot;alice@example.com&quot;, age=25)<\/code><\/pre>\n<h4>6.2 API\u5f00\u53d1<\/h4>\n<p>\u5728FastAPI\u7b49\u6846\u67b6\u4e2d\uff0c\u7c7b\u578b\u6ce8\u89e3\u7528\u4e8e\u81ea\u52a8\u751f\u6210API\u6587\u6863\u548c\u8bf7\u6c42\u9a8c\u8bc1\uff1a<\/p>\n<pre><code class=\"python\">from fastapi import FastAPI\nfrom typing import List\n\napp = FastAPI()\n\n@app.post(&quot;\/items\/&quot;)\ndef create_item(name: str, price: float, tags: List[str] = None):\n    return {&quot;name&quot;: name, &quot;price&quot;: price, &quot;tags&quot;: tags}<\/code><\/pre>\n<h4>6.3 \u7c7b\u578b\u5316\u7684\u914d\u7f6e\u7ba1\u7406<\/h4>\n<pre><code class=\"python\">from typing import Dict, Any\nfrom dataclasses import dataclass\n\n@dataclass\nclass DatabaseConfig:\n    host: str\n    port: int\n    username: str\n    password: str\n\n@dataclass\nclass AppConfig:\n    debug: bool\n    database: DatabaseConfig\n    secret_key: str\n\nconfig: AppConfig = AppConfig(\n    debug=True,\n    database=DatabaseConfig(\n        host=&quot;localhost&quot;,\n        port=5432,\n        username=&quot;admin&quot;,\n        password=&quot;secret&quot;\n    ),\n    secret_key=&quot;supersecret&quot;\n)<\/code><\/pre>\n<h3>7. \u7c7b\u578b\u6ce8\u89e3\u7684\u6027\u80fd\u5f71\u54cd<\/h3>\n<h4>7.1 \u8fd0\u884c\u65f6\u5f00\u9500<\/h4>\n<ul>\n<li>\u7c7b\u578b\u6ce8\u89e3\u5728\u8fd0\u884c\u65f6\u5b58\u50a8\u5728<code>__annotations__<\/code>\u5c5e\u6027\u4e2d<\/li>\n<li>\u57fa\u672c\u7c7b\u578b\u6ce8\u89e3\u7684\u8fd0\u884c\u65f6\u5f00\u9500\u6781\u5c0f<\/li>\n<li>\u590d\u6742\u6cdb\u578b\u7c7b\u578b\u53ef\u80fd\u4f1a\u6709\u8f7b\u5fae\u7684\u5185\u5b58\u5f00\u9500<\/li>\n<\/ul>\n<h4>7.2 \u7f16\u8bd1\u65f6\u4f18\u5316<\/h4>\n<ul>\n<li>\u4e00\u4e9bJIT\u7f16\u8bd1\u5668\uff08\u5982PyPy\uff09\u53ef\u4ee5\u5229\u7528\u7c7b\u578b\u6ce8\u89e3\u8fdb\u884c\u4f18\u5316<\/li>\n<li>\u9759\u6001\u7c7b\u578b\u68c0\u67e5\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u6355\u83b7\u9519\u8bef\uff0c\u51cf\u5c11\u8fd0\u884c\u65f6\u9519\u8bef<\/li>\n<\/ul>\n<h3>8. \u672a\u6765\u53d1\u5c55\u8d8b\u52bf<\/h3>\n<h4>8.1 PEP 646\uff1a\u53ef\u53d8\u6cdb\u578b<\/h4>\n<p>\u5141\u8bb8\u66f4\u7075\u6d3b\u7684\u6cdb\u578b\u7c7b\u578b\u5b9a\u4e49\uff0c\u652f\u6301\u4efb\u610f\u6570\u91cf\u7684\u7c7b\u578b\u53c2\u6570\u3002<\/p>\n<h4>8.2 PEP 673\uff1a<code>Self<\/code>\u7c7b\u578b<\/h4>\n<p>\u7b80\u5316\u7c7b\u65b9\u6cd5\u7684\u8fd4\u56de\u7c7b\u578b\u6ce8\u89e3\uff1a<\/p>\n<pre><code class=\"python\">from typing import Self\n\nclass MyClass:\n    def method(self) -&gt; Self:\n        return self<\/code><\/pre>\n<h4>8.3 PEP 688\uff1a<code>LiteralString<\/code>\u7c7b\u578b<\/h4>\n<p>\u7528\u4e8e\u6807\u8bb0\u5b57\u9762\u91cf\u5b57\u7b26\u4e32\uff0c\u589e\u5f3a\u7c7b\u578b\u5b89\u5168\u3002<\/p>\n<h3>9. \u603b\u7ed3<\/h3>\n<p>Python\u7684\u7c7b\u578b\u7cfb\u7edf\u548c\u7c7b\u578b\u6ce8\u89e3\u662f\u73b0\u4ee3Python\u5f00\u53d1\u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u7c7b\u578b\u6ce8\u89e3\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\uff1a<\/p>\n<ol>\n<li><strong>\u63d0\u9ad8\u4ee3\u7801\u53ef\u8bfb\u6027<\/strong>\uff1a\u7c7b\u578b\u6ce8\u89e3\u4f5c\u4e3a\u4e00\u79cd\u6587\u6863\u5f62\u5f0f\uff0c\u6e05\u6670\u8868\u8fbe\u51fd\u6570\u548c\u53d8\u91cf\u7684\u9884\u671f\u7c7b\u578b<\/li>\n<li><strong>\u51cf\u5c11\u7c7b\u578b\u9519\u8bef<\/strong>\uff1a\u9759\u6001\u7c7b\u578b\u68c0\u67e5\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u6355\u83b7\u6f5c\u5728\u7684\u7c7b\u578b\u95ee\u9898<\/li>\n<li><strong>\u6539\u5584IDE\u652f\u6301<\/strong>\uff1a\u7c7b\u578b\u6ce8\u89e3\u4f7fIDE\u80fd\u591f\u63d0\u4f9b\u66f4\u51c6\u786e\u7684\u4ee3\u7801\u8865\u5168\u548c\u7c7b\u578b\u63d0\u793a<\/li>\n<li><strong>\u589e\u5f3a\u4ee3\u7801\u53ef\u7ef4\u62a4\u6027<\/strong>\uff1a\u7c7b\u578b\u4fe1\u606f\u4f7f\u4ee3\u7801\u66f4\u5bb9\u6613\u7406\u89e3\u548c\u91cd\u6784<\/li>\n<li><strong>\u4fc3\u8fdb\u56e2\u961f\u534f\u4f5c<\/strong>\uff1a\u7edf\u4e00\u7684\u7c7b\u578b\u6807\u6ce8\u98ce\u683c\u6709\u52a9\u4e8e\u56e2\u961f\u6210\u5458\u4e4b\u95f4\u7684\u6c9f\u901a<\/li>\n<\/ol>\n<p>\u968f\u7740Python\u7c7b\u578b\u7cfb\u7edf\u7684\u4e0d\u65ad\u5b8c\u5584\uff0c\u7c7b\u578b\u6ce8\u89e3\u5c06\u5728Python\u751f\u6001\u7cfb\u7edf\u4e2d\u53d1\u6325\u8d8a\u6765\u8d8a\u91cd\u8981\u7684\u4f5c\u7528\u3002\u5bf9\u4e8e\u5927\u578b\u9879\u76ee\u548c\u56e2\u961f\u6765\u8bf4\uff0c\u91c7\u7528\u7c7b\u578b\u6ce8\u89e3\u5df2\u7ecf\u6210\u4e3a\u4e00\u79cd\u6700\u4f73\u5b9e\u8df5\uff0c\u80fd\u591f\u663e\u8457\u63d0\u9ad8\u4ee3\u7801\u8d28\u91cf\u548c\u5f00\u53d1\u6548\u7387\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000047585267\">https:\/\/segmentfault.com\/a\/1190000047585267<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u4e2d\u7684\u7c7b\u578b\u7cfb\u7edf\u4e0e\u7c7b\u578b\u6ce8\u89e3\u8fdb\u9636 1. \u7c7b\u578b\u7cfb\u7edf\u6982\u8ff0 1.1 \u52a8\u6001\u7c7b\u578b\u4e0e\u9759\u6001\u7c7b\u578b Python\u4f5c\u4e3a\u4e00\u79cd\u52a8 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10021226","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021226","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10021226"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021226\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10021226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10021226"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10021226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}