{"id":1153465,"date":"2025-01-13T17:39:07","date_gmt":"2025-01-13T09:39:07","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1153465.html"},"modified":"2025-01-13T17:39:09","modified_gmt":"2025-01-13T09:39:09","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%85%b3%e8%81%94%e5%8f%98%e9%87%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1153465.html","title":{"rendered":"\u5982\u4f55\u7528python\u5173\u8054\u53d8\u91cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25183222\/d2f554e6-1ec6-4142-bfe7-fc68680e7895.webp\" alt=\"\u5982\u4f55\u7528python\u5173\u8054\u53d8\u91cf\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u5173\u8054\u53d8\u91cf\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c<strong>\u53ef\u4ee5\u4f7f\u7528\u57fa\u7840\u7684\u8d4b\u503c\u64cd\u4f5c\u3001\u4f7f\u7528\u5217\u8868\u6216\u5b57\u5178\u3001\u901a\u8fc7\u7c7b\u548c\u5bf9\u8c61\u3001\u4f7f\u7528\u95ed\u5305\u3001\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u3001\u4f7f\u7528\u5e93\u5982Pandas\u548cNumpy<\/strong>\u7b49\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61<\/strong>\u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u65b9\u6cd5\uff0c\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u5173\u8054\u53d8\u91cf\uff0c\u8fd8\u53ef\u4ee5\u5c01\u88c5\u6570\u636e\u548c\u884c\u4e3a\uff0c\u521b\u5efa\u66f4\u590d\u6742\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><p>\u5f53\u4f60\u9700\u8981\u5173\u8054\u591a\u4e2a\u53d8\u91cf\u5e76\u4e14\u8fd9\u4e9b\u53d8\u91cf\u6709\u4e00\u5b9a\u7684\u903b\u8f91\u5173\u7cfb\u65f6\uff0c\u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61\u662f\u4e00\u4e2a\u6781\u597d\u7684\u9009\u62e9\u3002\u7c7b\u53ef\u4ee5\u5c06\u6570\u636e\u548c\u76f8\u5173\u7684\u884c\u4e3a\u5c01\u88c5\u5728\u4e00\u8d77\uff0c\u4f7f\u4ee3\u7801\u66f4\u6e05\u6670\u3001\u66f4\u6613\u4e8e\u7ef4\u62a4\u3002\u901a\u8fc7\u5b9a\u4e49\u7c7b\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u5177\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u7684\u5bf9\u8c61\uff0c\u8fd9\u4e9b\u5c5e\u6027\u548c\u65b9\u6cd5\u53ef\u4ee5\u76f8\u4e92\u5173\u8054\uff0c\u4ece\u800c\u5b9e\u73b0\u590d\u6742\u7684\u6570\u636e\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61\u6765\u5173\u8054\u53d8\u91cf\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b9a\u4e49\u7c7b\u548c\u521b\u5efa\u5bf9\u8c61<\/h3>\n<\/p>\n<p><h4>1\u3001\u5b9a\u4e49\u7c7b<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u3002\u5728\u7c7b\u4e2d\uff0c\u4f60\u53ef\u4ee5\u5b9a\u4e49\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u5c5e\u6027\u662f\u4e0e\u7c7b\u76f8\u5173\u7684\u6570\u636e\uff0c\u65b9\u6cd5\u662f\u4e0e\u7c7b\u76f8\u5173\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Car:<\/p>\n<p>    def __init__(self, brand, model, year):<\/p>\n<p>        self.brand = brand<\/p>\n<p>        self.model = model<\/p>\n<p>        self.year = year<\/p>\n<p>    def display_info(self):<\/p>\n<p>        print(f&quot;{self.year} {self.brand} {self.model}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>Car<\/code>\u7684\u7c7b\u3002<code>__init__<\/code>\u65b9\u6cd5\u662f\u6784\u9020\u51fd\u6570\uff0c\u7528\u4e8e\u521d\u59cb\u5316\u5bf9\u8c61\u7684\u5c5e\u6027\u3002<code>display_info<\/code>\u65b9\u6cd5\u662f\u7c7b\u7684\u65b9\u6cd5\uff0c\u7528\u4e8e\u6253\u5370\u6c7d\u8f66\u7684\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u521b\u5efa\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u5b9a\u4e49\u597d\u7c7b\u540e\uff0c\u53ef\u4ee5\u521b\u5efa\u5bf9\u8c61\u5e76\u5173\u8054\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_car = Car(&quot;Toyota&quot;, &quot;Corolla&quot;, 2020)<\/p>\n<p>my_car.display_info()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u521b\u5efa<code>Car<\/code>\u7c7b\u7684\u5bf9\u8c61<code>my_car<\/code>\uff0c\u5c06\u54c1\u724c\u3001\u578b\u53f7\u548c\u5e74\u4efd\u5173\u8054\u5230\u4e00\u8d77\uff0c\u5e76\u8c03\u7528<code>display_info<\/code>\u65b9\u6cd5\u663e\u793a\u6c7d\u8f66\u7684\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u6216\u5b57\u5178<\/h3>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u53d8\u91cf\u7684\u5173\u8054\u5173\u7cfb\u8f83\u4e3a\u7b80\u5355\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">car1 = [&quot;Toyota&quot;, &quot;Corolla&quot;, 2020]<\/p>\n<p>car2 = [&quot;Honda&quot;, &quot;Civic&quot;, 2019]<\/p>\n<p>def display_car_info(car):<\/p>\n<p>    print(f&quot;{car[2]} {car[0]} {car[1]}&quot;)<\/p>\n<p>display_car_info(car1)<\/p>\n<p>display_car_info(car2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528\u5217\u8868\u5b58\u50a8\u6c7d\u8f66\u7684\u54c1\u724c\u3001\u578b\u53f7\u548c\u5e74\u4efd\uff0c\u5e76\u901a\u8fc7\u51fd\u6570<code>display_car_info<\/code>\u6765\u663e\u793a\u6c7d\u8f66\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u4e5f\u662f\u4e00\u79cd\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5173\u8054\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">car1 = {&quot;brand&quot;: &quot;Toyota&quot;, &quot;model&quot;: &quot;Corolla&quot;, &quot;year&quot;: 2020}<\/p>\n<p>car2 = {&quot;brand&quot;: &quot;Honda&quot;, &quot;model&quot;: &quot;Civic&quot;, &quot;year&quot;: 2019}<\/p>\n<p>def display_car_info(car):<\/p>\n<p>    print(f&quot;{car[&#39;year&#39;]} {car[&#39;brand&#39;]} {car[&#39;model&#39;]}&quot;)<\/p>\n<p>display_car_info(car1)<\/p>\n<p>display_car_info(car2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528\u5b57\u5178\u5b58\u50a8\u6c7d\u8f66\u7684\u4fe1\u606f\uff0c\u5e76\u901a\u8fc7\u51fd\u6570<code>display_car_info<\/code>\u6765\u663e\u793a\u6c7d\u8f66\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u95ed\u5305<\/h3>\n<\/p>\n<p><p>\u95ed\u5305\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u5b83\u80fd\u591f\u6355\u83b7\u5e76\u4fdd\u5b58\u5176\u6240\u5728\u73af\u5883\u4e2d\u7684\u53d8\u91cf\u3002\u53ef\u4ee5\u5229\u7528\u95ed\u5305\u6765\u5173\u8054\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def make_car(brand, model, year):<\/p>\n<p>    def display_info():<\/p>\n<p>        print(f&quot;{year} {brand} {model}&quot;)<\/p>\n<p>    return display_info<\/p>\n<p>car_info = make_car(&quot;Toyota&quot;, &quot;Corolla&quot;, 2020)<\/p>\n<p>car_info()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>make_car<\/code>\u51fd\u6570\u521b\u5efa\u4e86\u4e00\u4e2a\u95ed\u5305\uff0c\u6355\u83b7\u5e76\u4fdd\u5b58\u4e86<code>brand<\/code>\u3001<code>model<\/code>\u548c<code>year<\/code>\u53d8\u91cf\u3002\u8fd4\u56de\u7684<code>display_info<\/code>\u51fd\u6570\u53ef\u4ee5\u8bbf\u95ee\u8fd9\u4e9b\u53d8\u91cf\uff0c\u4ece\u800c\u5173\u8054\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5168\u5c40\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u9700\u8981\u5728\u591a\u4e2a\u51fd\u6570\u6216\u7c7b\u4e4b\u95f4\u5171\u4eab\u53d8\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">brand = &quot;Toyota&quot;<\/p>\n<p>model = &quot;Corolla&quot;<\/p>\n<p>year = 2020<\/p>\n<p>def display_info():<\/p>\n<p>    global brand, model, year<\/p>\n<p>    print(f&quot;{year} {brand} {model}&quot;)<\/p>\n<p>display_info()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528\u5168\u5c40\u53d8\u91cf<code>brand<\/code>\u3001<code>model<\/code>\u548c<code>year<\/code>\uff0c\u5e76\u901a\u8fc7<code>display_info<\/code>\u51fd\u6570\u6765\u663e\u793a\u6c7d\u8f66\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528Pandas\u548cNumpy<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6570\u636e\u5206\u6790\u548c\u5904\u7406\uff0cPandas\u548cNumpy\u662f\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528Pandas<\/h4>\n<\/p>\n<p><p>Pandas\u63d0\u4f9b\u4e86DataFrame\u7ed3\u6784\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5173\u8054\u548c\u64cd\u4f5c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {<\/p>\n<p>    &quot;brand&quot;: [&quot;Toyota&quot;, &quot;Honda&quot;],<\/p>\n<p>    &quot;model&quot;: [&quot;Corolla&quot;, &quot;Civic&quot;],<\/p>\n<p>    &quot;year&quot;: [2020, 2019]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528Pandas\u521b\u5efa\u4e86\u4e00\u4e2aDataFrame\uff0c\u5e76\u5b58\u50a8\u6c7d\u8f66\u7684\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528Numpy<\/h4>\n<\/p>\n<p><p>Numpy\u63d0\u4f9b\u4e86\u6570\u7ec4\u7ed3\u6784\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\u548c\u6570\u636e\u5173\u8054\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>data = np.array([[&quot;Toyota&quot;, &quot;Corolla&quot;, 2020], [&quot;Honda&quot;, &quot;Civic&quot;, 2019]])<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528Numpy\u521b\u5efa\u4e86\u4e00\u4e2a\u6570\u7ec4\uff0c\u5e76\u5b58\u50a8\u6c7d\u8f66\u7684\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u5b9e\u73b0\u53d8\u91cf\u7684\u5173\u8054\u3002<strong>\u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61\u3001\u5217\u8868\u6216\u5b57\u5178\u3001\u95ed\u5305\u3001\u5168\u5c40\u53d8\u91cf\u3001Pandas\u548cNumpy<\/strong>\u90fd\u53ef\u4ee5\u65b9\u4fbf\u5730\u5173\u8054\u548c\u64cd\u4f5c\u53d8\u91cf\u3002\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u6df1\u5165\u63a2\u8ba8\u7c7b\u548c\u5bf9\u8c61<\/h3>\n<\/p>\n<p><h4>1\u3001\u7ee7\u627f\u548c\u591a\u6001<\/h4>\n<\/p>\n<p><p>\u5728\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\uff0c\u7ee7\u627f\u548c\u591a\u6001\u662f\u4e24\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u6982\u5ff5\u3002\u7ee7\u627f\u5141\u8bb8\u4f60\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7c7b\uff0c\u8be5\u7c7b\u57fa\u4e8e\u4e00\u4e2a\u73b0\u6709\u7684\u7c7b\uff0c\u4ece\u800c\u91cd\u7528\u548c\u6269\u5c55\u73b0\u6709\u7684\u4ee3\u7801\u3002\u591a\u6001\u5141\u8bb8\u4f60\u4f7f\u7528\u4e0d\u540c\u7c7b\u7684\u5bf9\u8c61\u901a\u8fc7\u76f8\u540c\u7684\u63a5\u53e3\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Vehicle:<\/p>\n<p>    def __init__(self, brand, model, year):<\/p>\n<p>        self.brand = brand<\/p>\n<p>        self.model = model<\/p>\n<p>        self.year = year<\/p>\n<p>    def display_info(self):<\/p>\n<p>        print(f&quot;{self.year} {self.brand} {self.model}&quot;)<\/p>\n<p>class Car(Vehicle):<\/p>\n<p>    def __init__(self, brand, model, year, doors):<\/p>\n<p>        super().__init__(brand, model, year)<\/p>\n<p>        self.doors = doors<\/p>\n<p>    def display_info(self):<\/p>\n<p>        super().display_info()<\/p>\n<p>        print(f&quot;Doors: {self.doors}&quot;)<\/p>\n<p>class Truck(Vehicle):<\/p>\n<p>    def __init__(self, brand, model, year, capacity):<\/p>\n<p>        super().__init__(brand, model, year)<\/p>\n<p>        self.capacity = capacity<\/p>\n<p>    def display_info(self):<\/p>\n<p>        super().display_info()<\/p>\n<p>        print(f&quot;Capacity: {self.capacity} tons&quot;)<\/p>\n<p>my_car = Car(&quot;Toyota&quot;, &quot;Corolla&quot;, 2020, 4)<\/p>\n<p>my_truck = Truck(&quot;Ford&quot;, &quot;F-150&quot;, 2018, 1.5)<\/p>\n<p>my_car.display_info()<\/p>\n<p>my_truck.display_info()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u57fa\u7c7b<code>Vehicle<\/code>\uff0c\u5e76\u4ece<code>Vehicle<\/code>\u7c7b\u6d3e\u751f\u4e86\u4e24\u4e2a\u5b50\u7c7b<code>Car<\/code>\u548c<code>Truck<\/code>\u3002\u5b50\u7c7b\u6269\u5c55\u4e86\u57fa\u7c7b\u7684\u529f\u80fd\uff0c\u5e76\u91cd\u5199\u4e86<code>display_info<\/code>\u65b9\u6cd5\u3002\u901a\u8fc7\u7ee7\u627f\u548c\u591a\u6001\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5173\u8054\u548c\u64cd\u4f5c\u4e0d\u540c\u7c7b\u578b\u7684\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5c01\u88c5\u548c\u4fe1\u606f\u9690\u85cf<\/h4>\n<\/p>\n<p><p>\u5c01\u88c5\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u53e6\u4e00\u91cd\u8981\u6982\u5ff5\u3002\u5c01\u88c5\u5141\u8bb8\u4f60\u5c06\u6570\u636e\u548c\u884c\u4e3a\u5c01\u88c5\u5728\u7c7b\u4e2d\uff0c\u4ece\u800c\u9690\u85cf\u5185\u90e8\u5b9e\u73b0\u7ec6\u8282\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class BankAccount:<\/p>\n<p>    def __init__(self, account_number, balance):<\/p>\n<p>        self.account_number = account_number<\/p>\n<p>        self.__balance = balance<\/p>\n<p>    def deposit(self, amount):<\/p>\n<p>        if amount &gt; 0:<\/p>\n<p>            self.__balance += amount<\/p>\n<p>            print(f&quot;Deposited {amount}, new balance is {self.__balance}&quot;)<\/p>\n<p>    def withdraw(self, amount):<\/p>\n<p>        if 0 &lt; amount &lt;= self.__balance:<\/p>\n<p>            self.__balance -= amount<\/p>\n<p>            print(f&quot;Withdrew {amount}, new balance is {self.__balance}&quot;)<\/p>\n<p>        else:<\/p>\n<p>            print(&quot;Invalid withdrawal amount&quot;)<\/p>\n<p>    def get_balance(self):<\/p>\n<p>        return self.__balance<\/p>\n<p>account = BankAccount(&quot;123456&quot;, 1000)<\/p>\n<p>account.deposit(500)<\/p>\n<p>account.withdraw(300)<\/p>\n<p>print(f&quot;Final balance is {account.get_balance()}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>BankAccount<\/code>\u7c7b\u5c01\u88c5\u4e86\u8d26\u6237\u7684\u4f59\u989d\uff0c\u5e76\u901a\u8fc7<code>deposit<\/code>\u3001<code>withdraw<\/code>\u548c<code>get_balance<\/code>\u65b9\u6cd5\u6765\u64cd\u4f5c\u548c\u8bbf\u95ee\u4f59\u989d\u3002\u901a\u8fc7\u4f7f\u7528\u53cc\u4e0b\u5212\u7ebf\uff08<code>__balance<\/code>\uff09\uff0c\u9690\u85cf\u4e86\u4f59\u989d\u7684\u5185\u90e8\u5b9e\u73b0\u7ec6\u8282\uff0c\u786e\u4fdd\u4e86\u6570\u636e\u7684\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u88c5\u9970\u5668<\/h3>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u5728\u4e0d\u4fee\u6539\u51fd\u6570\u5b9a\u4e49\u7684\u524d\u63d0\u4e0b\uff0c\u52a8\u6001\u5730\u589e\u52a0\u6216\u4fee\u6539\u51fd\u6570\u7684\u529f\u80fd\u3002\u88c5\u9970\u5668\u53ef\u4ee5\u7528\u4e8e\u5173\u8054\u53d8\u91cf\uff0c\u5e76\u6dfb\u52a0\u989d\u5916\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def log_decorator(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        print(f&quot;Calling function &#39;{func.__name__}&#39; with arguments {args} {kwargs}&quot;)<\/p>\n<p>        result = func(*args, kwargs)<\/p>\n<p>        print(f&quot;Function &#39;{func.__name__}&#39; returned {result}&quot;)<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<p>@log_decorator<\/p>\n<p>def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>result = add(3, 5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>log_decorator<\/code>\u7684\u88c5\u9970\u5668\uff0c\u7528\u4e8e\u8bb0\u5f55\u51fd\u6570\u8c03\u7528\u548c\u8fd4\u56de\u7684\u7ed3\u679c\u3002\u901a\u8fc7\u4f7f\u7528\u88c5\u9970\u5668\uff0c\u53ef\u4ee5\u5728\u4e0d\u4fee\u6539\u539f\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\uff0c\u52a8\u6001\u5730\u589e\u52a0\u65e5\u5fd7\u8bb0\u5f55\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h3>\n<\/p>\n<p><p>\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u5bf9\u8c61\uff0c\u7528\u4e8e\u7ba1\u7406\u8d44\u6e90\u7684\u5206\u914d\u548c\u91ca\u653e\u3002\u53ef\u4ee5\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u6765\u5173\u8054\u53d8\u91cf\uff0c\u5e76\u786e\u4fdd\u8d44\u6e90\u7684\u6b63\u786e\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class FileManager:<\/p>\n<p>    def __init__(self, filename, mode):<\/p>\n<p>        self.filename = filename<\/p>\n<p>        self.mode = mode<\/p>\n<p>    def __enter__(self):<\/p>\n<p>        self.file = open(self.filename, self.mode)<\/p>\n<p>        return self.file<\/p>\n<p>    def __exit__(self, exc_type, exc_val, exc_tb):<\/p>\n<p>        self.file.close()<\/p>\n<p>with FileManager(&quot;example.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    file.write(&quot;Hello, world!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>FileManager<\/code>\u7684\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u7c7b\uff0c\u7528\u4e8e\u7ba1\u7406\u6587\u4ef6\u7684\u6253\u5f00\u548c\u5173\u95ed\u3002\u901a\u8fc7\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0c\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u6b63\u786e\u5730\u5173\u95ed\uff0c\u4ece\u800c\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u4f7f\u7528\u751f\u6210\u5668<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u8fed\u4ee3\u5668\uff0c\u7528\u4e8e\u751f\u6210\u5e8f\u5217\u4e2d\u7684\u5143\u7d20\u3002\u751f\u6210\u5668\u53ef\u4ee5\u7528\u4e8e\u5173\u8054\u53d8\u91cf\uff0c\u5e76\u52a8\u6001\u5730\u751f\u6210\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def countdown(n):<\/p>\n<p>    print(&quot;Starting countdown&quot;)<\/p>\n<p>    while n &gt; 0:<\/p>\n<p>        yield n<\/p>\n<p>        n -= 1<\/p>\n<p>for i in countdown(5):<\/p>\n<p>    print(i)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>countdown<\/code>\u7684\u751f\u6210\u5668\u51fd\u6570\uff0c\u7528\u4e8e\u751f\u6210\u4ece<code>n<\/code>\u52301\u7684\u5012\u8ba1\u65f6\u5e8f\u5217\u3002\u901a\u8fc7\u4f7f\u7528\u751f\u6210\u5668\uff0c\u53ef\u4ee5\u52a8\u6001\u5730\u751f\u6210\u6570\u636e\uff0c\u5e76\u5728\u9700\u8981\u65f6\u8fdb\u884c\u5173\u8054\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u4f7f\u7528\u5143\u7c7b<\/h3>\n<\/p>\n<p><p>\u5143\u7c7b\u662f\u7528\u4e8e\u521b\u5efa\u7c7b\u7684\u7c7b\u3002\u5143\u7c7b\u5141\u8bb8\u4f60\u63a7\u5236\u7c7b\u7684\u521b\u5efa\u548c\u884c\u4e3a\uff0c\u53ef\u4ee5\u7528\u4e8e\u5173\u8054\u53d8\u91cf\uff0c\u5e76\u52a8\u6001\u5730\u751f\u6210\u7c7b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Singleton(type):<\/p>\n<p>    _instances = {}<\/p>\n<p>    def __call__(cls, *args, kwargs):<\/p>\n<p>        if cls not in cls._instances:<\/p>\n<p>            cls._instances[cls] = super().__call__(*args, kwargs)<\/p>\n<p>        return cls._instances[cls]<\/p>\n<p>class MyClass(metaclass=Singleton):<\/p>\n<p>    def __init__(self, value):<\/p>\n<p>        self.value = value<\/p>\n<p>a = MyClass(1)<\/p>\n<p>b = MyClass(2)<\/p>\n<p>print(a.value)<\/p>\n<p>print(b.value)<\/p>\n<p>print(a is b)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>Singleton<\/code>\u7684\u5143\u7c7b\uff0c\u7528\u4e8e\u521b\u5efa\u5355\u4f8b\u7c7b\u3002\u901a\u8fc7\u4f7f\u7528\u5143\u7c7b\uff0c\u53ef\u4ee5\u63a7\u5236\u7c7b\u7684\u521b\u5efa\u884c\u4e3a\uff0c\u5e76\u786e\u4fdd\u7c7b\u5728\u6574\u4e2a\u5e94\u7528\u7a0b\u5e8f\u4e2d\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u4f7f\u7528\u534f\u7a0b<\/h3>\n<\/p>\n<p><p>\u534f\u7a0b\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\u6682\u505c\u548c\u6062\u590d\u3002\u534f\u7a0b\u53ef\u4ee5\u7528\u4e8e\u5173\u8054\u53d8\u91cf\uff0c\u5e76\u5b9e\u73b0\u5f02\u6b65\u7f16\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>async def greet(name):<\/p>\n<p>    print(f&quot;Hello, {name}&quot;)<\/p>\n<p>    aw<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t asyncio.sleep(1)<\/p>\n<p>    print(f&quot;Goodbye, {name}&quot;)<\/p>\n<p>async def main():<\/p>\n<p>    await asyncio.gather(greet(&quot;Alice&quot;), greet(&quot;Bob&quot;))<\/p>\n<p>asyncio.run(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>greet<\/code>\u7684\u534f\u7a0b\u51fd\u6570\uff0c\u7528\u4e8e\u5f02\u6b65\u5730\u6253\u5370\u95ee\u5019\u8bed\u548c\u544a\u522b\u8bed\u3002\u901a\u8fc7\u4f7f\u7528\u534f\u7a0b\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5f02\u6b65\u7f16\u7a0b\uff0c\u5e76\u5728\u9700\u8981\u65f6\u8fdb\u884c\u5173\u8054\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u4f7f\u7528\u5c5e\u6027\u548c\u63cf\u8ff0\u7b26<\/h3>\n<\/p>\n<p><p>\u5c5e\u6027\u548c\u63cf\u8ff0\u7b26\u662fPython\u4e2d\u7528\u4e8e\u7ba1\u7406\u5bf9\u8c61\u5c5e\u6027\u7684\u4e00\u79cd\u673a\u5236\u3002\u53ef\u4ee5\u4f7f\u7528\u5c5e\u6027\u548c\u63cf\u8ff0\u7b26\u6765\u5173\u8054\u53d8\u91cf\uff0c\u5e76\u63a7\u5236\u5c5e\u6027\u7684\u8bbf\u95ee\u548c\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Celsius:<\/p>\n<p>    def __init__(self, temperature=0):<\/p>\n<p>        self._temperature = temperature<\/p>\n<p>    def to_fahrenheit(self):<\/p>\n<p>        return (self._temperature * 9\/5) + 32<\/p>\n<p>    @property<\/p>\n<p>    def temperature(self):<\/p>\n<p>        print(&quot;Getting temperature&quot;)<\/p>\n<p>        return self._temperature<\/p>\n<p>    @temperature.setter<\/p>\n<p>    def temperature(self, value):<\/p>\n<p>        if value &lt; -273.15:<\/p>\n<p>            raise ValueError(&quot;Temperature below -273.15 is not possible&quot;)<\/p>\n<p>        print(&quot;Setting temperature&quot;)<\/p>\n<p>        self._temperature = value<\/p>\n<p>c = Celsius()<\/p>\n<p>c.temperature = 25<\/p>\n<p>print(c.temperature)<\/p>\n<p>print(c.to_fahrenheit())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>Celsius<\/code>\u7684\u7c7b\uff0c\u5e76\u4f7f\u7528<code>@property<\/code>\u88c5\u9970\u5668\u5b9a\u4e49\u4e86\u6e29\u5ea6\u5c5e\u6027\u3002\u901a\u8fc7\u4f7f\u7528\u5c5e\u6027\u548c\u63cf\u8ff0\u7b26\uff0c\u53ef\u4ee5\u63a7\u5236\u5c5e\u6027\u7684\u8bbf\u95ee\u548c\u4fee\u6539\uff0c\u5e76\u5728\u9700\u8981\u65f6\u8fdb\u884c\u5173\u8054\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\u4ee5\u4e0a\u5185\u5bb9\uff0c\u5728Python\u4e2d\u5173\u8054\u53d8\u91cf\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c<strong>\u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61\u3001\u5217\u8868\u6216\u5b57\u5178\u3001\u95ed\u5305\u3001\u5168\u5c40\u53d8\u91cf\u3001Pandas\u548cNumpy\u3001\u88c5\u9970\u5668\u3001\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u3001\u751f\u6210\u5668\u3001\u5143\u7c7b\u3001\u534f\u7a0b\u3001\u5c5e\u6027\u548c\u63cf\u8ff0\u7b26<\/strong>\u7b49\u90fd\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u53d8\u91cf\u7684\u5173\u8054\u548c\u64cd\u4f5c\u3002\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u548c\u5e94\u7528\u573a\u666f\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3001\u53ef\u7ef4\u62a4\u6027\u548c\u6027\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9a\u4e49\u548c\u4f7f\u7528\u5173\u8054\u53d8\u91cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5173\u8054\u53d8\u91cf\u901a\u5e38\u6307\u7684\u662f\u901a\u8fc7\u67d0\u79cd\u5173\u7cfb\u6216\u903b\u8f91\u5c06\u591a\u4e2a\u53d8\u91cf\u8054\u7cfb\u5728\u4e00\u8d77\u3002\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u3001\u5217\u8868\u6216\u7c7b\u7b49\u6570\u636e\u7ed3\u6784\u6765\u5b9a\u4e49\u548c\u7ba1\u7406\u5173\u8054\u53d8\u91cf\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u5b57\u5178\u53ef\u4ee5\u5c06\u76f8\u5173\u8054\u7684\u4fe1\u606f\u5b58\u50a8\u4e3a\u952e\u503c\u5bf9\uff0c\u4f7f\u5f97\u8bbf\u95ee\u548c\u4fee\u6539\u8fd9\u4e9b\u53d8\u91cf\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u5173\u8054\u53d8\u91cf\u65f6\uff0c\u5982\u4f55\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff1f<\/strong><br \/>\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528\u6709\u610f\u4e49\u7684\u53d8\u91cf\u540d\uff0c\u5e76\u5728\u5fc5\u8981\u65f6\u6dfb\u52a0\u6ce8\u91ca\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u7c7b\u6765\u5c01\u88c5\u76f8\u5173\u53d8\u91cf\u548c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u5f97\u4ee3\u7801\u7ed3\u6784\u66f4\u52a0\u6e05\u6670\u3002\u4f8b\u5982\uff0c\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u6765\u8868\u793a\u5b66\u751f\u4fe1\u606f\uff0c\u5176\u4e2d\u5305\u542b\u59d3\u540d\u3001\u5e74\u9f84\u548c\u6210\u7ee9\u7b49\u5c5e\u6027\uff0c\u8fd9\u6837\u53ef\u4ee5\u4f7f\u5f97\u76f8\u5173\u53d8\u91cf\u66f4\u52a0\u660e\u786e\u548c\u6613\u4e8e\u7ba1\u7406\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u6709\u6548\u5730\u7ba1\u7406\u591a\u4e2a\u5173\u8054\u53d8\u91cf\u7684\u72b6\u6001\uff1f<\/strong><br \/>\u7ba1\u7406\u591a\u4e2a\u5173\u8054\u53d8\u91cf\u7684\u72b6\u6001\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61\u6765\u5b9e\u73b0\u3002\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u7c7b\uff0c\u60a8\u53ef\u4ee5\u5c06\u76f8\u5173\u7684\u53d8\u91cf\u548c\u5b83\u4eec\u7684\u64cd\u4f5c\u5c01\u88c5\u5728\u4e00\u8d77\uff0c\u4ece\u800c\u4f7f\u5f97\u72b6\u6001\u7ba1\u7406\u66f4\u52a0\u7cfb\u7edf\u5316\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u65b9\u6cd5\u6765\u66f4\u65b0\u8fd9\u4e9b\u53d8\u91cf\u7684\u503c\uff0c\u4e5f\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u4e00\u81f4\u6027\u548c\u6709\u6548\u6027\u3002\u4f7f\u7528\u6570\u636e\u7ed3\u6784\u5982\u5217\u8868\u6216\u5b57\u5178\u4e5f\u80fd\u5e2e\u52a9\u8ffd\u8e2a\u548c\u66f4\u65b0\u72b6\u6001\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5173\u8054\u53d8\u91cf\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u53ef\u4ee5\u4f7f\u7528\u57fa\u7840\u7684\u8d4b\u503c\u64cd\u4f5c\u3001\u4f7f\u7528\u5217\u8868\u6216\u5b57\u5178\u3001\u901a\u8fc7\u7c7b\u548c\u5bf9\u8c61\u3001\u4f7f\u7528\u95ed\u5305\u3001\u4f7f\u7528\u5168\u5c40 [&hellip;]","protected":false},"author":3,"featured_media":1153469,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1153465"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1153465"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1153465\/revisions"}],"predecessor-version":[{"id":1153470,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1153465\/revisions\/1153470"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1153469"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1153465"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1153465"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1153465"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}