{"id":248,"date":"2025-11-09T09:51:30","date_gmt":"2025-11-09T01:51:30","guid":{"rendered":"https:\/\/www.linerroom.cn\/?p=248"},"modified":"2025-11-09T10:13:12","modified_gmt":"2025-11-09T02:13:12","slug":"248","status":"publish","type":"post","link":"https:\/\/www.linerroom.cn\/?p=248","title":{"rendered":"python\u7684\u9ad8\u7ea7\u8bed\u6cd5\uff0810\uff09"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u4e00.Python\u4e2d\u7684\u88c5\u9970\u5668\u662f\u4ec0\u4e48?\u5982\u4f55\u4f7f\u7528?<\/h2>\n\n\n\n<p>\u88c5\u9970\u5668(Decorator):\u662f\u4e00\u79cd\u7528\u4e8e\u4fee\u6539\u6216\u6269\u5c55\u51fd\u6570\u6216\u65b9\u6cd5\u884c\u4e3a\u7684\u9ad8\u9636\u51fd\u6570\u3002\u88c5\u9970\u5668\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u5e76\u8fd4\u56de\u53e6\u4e00\u4e2a\u51fd\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def my_decorator(func):\n    def wrapper():\n        print(\"\u5728\u51fd\u6570\u6267\u884c\u4e4b\u524d\u505a\u4e00\u4e9b\u4e8b\u60c5\")\n        func()\n        print(\"\u5728\u51fd\u6570\u6267\u884c\u4e4b\u540e\u505a\u4e00\u4e9b\u4e8b\u60c5\")\n    return wrapper\n@my_decorator\ndef say_hello():\n    print(\"Hello!\")\n    \nsay_hello()\n# \u8f93\u51fa:\n#\u5728\u51fd\u6570\u6267\u884c\u4e4b\u524d\u505a\u4e00\u4e9b\u4e8b\u60c5\n# Hello!\n#\u5728\u51fd\u6570\u6267\u884c\u4e4b\u540e\u505a\u4e00\u4e9b\u4e8b\u60c5<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c.Python\u4e2d\u7684\u5217\u8868\u548c\u5143\u7ec4\u6709\u4ec0\u4e48\u533a\u522b?<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<ul class=\"wp-block-list\">\n<li>\u5217\u8868(List):\u662f\u53ef\u53d8\u7684(mutable)\uff0c\u5373\u53ef\u4ee5\u4fee\u6539\u3001\u6dfb\u52a0\u6216\u5220\u9664\u5143\u7d20\u3002\u5217\u8868\u4f7f\u7528\u65b9\u62ec\u53f7[\u5b9a\u4e49\u3002<\/li>\n\n\n\n<li>\u00b7\u5143\u7ec4(Tuple):\u662f\u4e0d\u53ef\u53d8\u7684(immutable)\uff0c\u5373\u4e00\u65e6\u521b\u5efa\u5c31\u4e0d\u80fd\u4fee\u6539\u3002\u5143\u7ec4\u4f7f\u7528\u5706\u62ec\u53f7()\u5b9a\u4e49\u3002<\/li>\n<\/ul>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code># \u5217\u8868\u793a\u4f8b\nmy_list=&#91;1,2,3]\nmy_list.append(4)# \u6dfb\u52a0\u5143\u7d20\nmy_list&#91;0]= 10# \u4fee\u6539\u5143\u7d20\n#\u8f93\u51fa:&#91;10\uff0c2\uff0c3\uff0c4]print(my_list)\n# \u5143\u7ec4\u793a\u4f8b\nmy_tuple =(1,2,3)\n#my_tuple&#91;0]=10 # \u8fd9\u884c\u4f1a\u62a5\u9519\uff0c\u56e0\u4e3a\u5143\u7ec4\u4e0d\u53ef\u53d8\nprint(my_tuple)#\u8f93\u51fa:(1\uff0c2\uff0c3)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09.Python\u4e2d\u7684\u751f\u6210\u5668\u662f\u4ec0\u4e48?\u5982\u4f55\u4f7f\u7528?<\/h2>\n\n\n\n<p>\u751f\u6210\u5668(Generator):\u662fPython\u4e2d\u7528\u4e8e\u6309\u9700\u751f\u6210\u503c\u7684\u5de5\u5177\uff0c\u5b83\u901a\u8fc7yield\u5173\u952e\u5b57\u5b9e\u73b0\u3002\u751f\u6210\u5668\u7684\u6838\u5fc3\u7279\u70b9\u662f\u60f0\u6027\u8ba1\u7b97\uff0c\u5373\u53ea\u6709\u5728\u9700\u8981\u65f6\u624d\u4f1a\u751f\u6210\u503c\uff0c\u4ece\u800c\u8282\u7701\u5185\u5b58\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def my_generator():\n    yield 1\n    yield 2\n    yield 3\ngen = my_generator()\nprint(next(gen))#\u8f93\u51fa:1\nprint(next(gen))# \u8f93\u51fa:2\nprint(next(gen))#\u8f93\u51fa:3<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db.Python\u4e2d\u7684with\u8bed\u53e5\u662f\u5982\u4f55\u5de5\u4f5c\u7684?<\/h2>\n\n\n\n<p>with\u8bed\u53e5:\u7528\u4e8e\u7b80\u5316\u8d44\u6e90\u7ba1\u7406\uff0c\u786e\u4fdd\u5728\u4f7f\u7528\u5b8c\u8d44\u6e90\u540e\u6b63\u786e\u91ca\u653e\u3002\u5b83\u901a\u5e38\u7528\u4e8e\u6587\u4ef6\u64cd\u4f5c\u3001\u6570\u636e\u5e93\u8fde\u63a5\u7b49\u573a\u666f\u3002with\u8bed\u53e5\u4f1a\u81ea\u52a8\u8c03\u7528\u5bf9\u8c61\u7684enter\u548cexit\u65b9\u6cd5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>with open('example.txt','r')as file:\n    content = file.read()\n    print(content)\n#\u6587\u4ef6\u4f1a\u5728with\u5757\u7ed3\u675f\u540e\u81ea\u52a8\u5173\u95ed<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94.Python\u4e2d\u7684<em>*args\u548c<\/em>**kwargs\u662f\u4ec0\u4e48?<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>*<em>args \u7528\u4e8e\u63a5\u6536\u4efb\u610f\u6570\u91cf\u7684\u4f4d\u7f6e\u53c2\u6570(\u975e\u5173\u952e\u5b57\u53c2\u6570)\uff0c\u901a\u5e38\u4ee5\u5143\u7ec4\u5f62\u5f0f\u4f20\u9012<\/em><\/li>\n\n\n\n<li>**kwargs \u7528\u4e8e\u63a5\u6536\u4efb\u610f\u6570\u91cf\u7684\u5173\u952e\u5b57\u53c2\u6570(\u952e\u503c\u5bf9\u5f62\u5f0f\u7684\u53c2\u6570)\uff0c\u901a\u5e38\u4ee5\u5b57\u5178\u5f62\u5f0f\u4f20\u9012\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u6ce8\u610f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e00\u4e2a<em>:\u7528\u4e8e\u5904\u7406\u4f4d\u7f6e\u53c2\u6570\uff0c\u8868\u793a\u5c06\u591a\u4e2a\u4f4d\u7f6e\u53c2\u6570\u6253\u5305\u6210\u4e00\u4e2a\u5143\u7ec4 <\/em><\/li>\n\n\n\n<li><em>\u4e24\u4e2a<\/em>*:\u7528\u4e8e\u5904\u7406\u5173\u952e\u5b57\u53c2\u6570\uff0c\u8868\u793a\u5c06\u591a\u4e2a\u5173\u952e\u5b57\u53c2\u6570\u6253\u5305\u6210\u4e00\u4e2a\u5b57\u5178\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>\ndef my_function(*args, **kwargs):\n    print(\"args:\",args)\n    print(\"kwargs:\",kwargs)\nmy_function(1,2,3,name=\"Alice\",age=25)\n# \u8f93\u51fa:\n# args:(1\uff0c2\uff0c3)\n# kwargs:{'name':Alice','age': 25}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d.Python\u4e2d\u7684GIL(\u5168\u5c40\u89e3\u91ca\u5668\u9501)\u662f\u4ec0\u4e48?<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>GIL<\/strong>(GlobalInterpreter Lock):\u662fCPython\u89e3\u91ca\u5668\u4e2d\u7684\u4e00\u4e2a\u4e92\u65a5\u9501\uff0c\u786e\u4fdd\u540c\u4e00\u65f6\u95f4\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884cPython\u5b57\u8282\u7801\u3002GIL\u662fCPython \u89e3\u91ca\u5668(Python \u7684\u9ed8\u8ba4\u5b9e\u73b0)\u7279\u6709\u7684\u673a\u5236\uff0c\u5176\u4ed6\u5b9e\u73b0(\u5982 Jython\u3001lronPython)\u6ca1\u6709 GIL<\/li>\n\n\n\n<li><strong>\u4f5c\u7528<\/strong>:GIL \u7684\u4e3b\u8981\u4f5c\u7528\u662f\u4fdd\u62a4 Python \u5185\u90e8\u6570\u636e\u7ed3\u6784(\u5982\u5bf9\u8c61\u5f15\u7528\u8ba1\u6570)\u7684\u7ebf\u7a0b\u5b89\u5168\u3002\u7531\u4e8ePython \u4f7f\u7528\u5f15\u7528\u8ba1\u6570\u6765\u7ba1\u7406\u5185\u5b58\uff0c\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u4fee\u6539\u5f15\u7528\u8ba1\u6570\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u6216\u9519\u8bef\u3002GIL \u901a\u8fc7\u5f3a\u5236\u540c\u4e00\u65f6\u95f4\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884c Python \u5b57\u8282\u7801\u6765\u907f\u514d\u8fd9\u4e9b\u95ee\u9898\u3002<\/li>\n<\/ul>\n\n\n\n<p> \u5de5\u4f5c\u539f\u7406:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>GIL \u662f\u4e00\u4e2a\u5168\u5c40\u9501\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u5728\u6267\u884c Python \u5b57\u8282\u7801\u4e4b\u524d\u5fc5\u987b\u5148\u83b7\u53d6 GIL\u3002<\/li>\n\n\n\n<li> \u5f53\u4e00\u4e2a\u7ebf\u7a0b\u83b7\u53d6 GIL\u540e\uff0c\u5176\u4ed6\u7ebf\u7a0b\u5fc5\u987b\u7b49\u5f85\u8be5\u7ebf\u7a0b\u91ca\u653e G\u513fL \u624d\u80fd\u7ee7\u7eed\u6267\u884c\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u4f18\u70b9:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7b80\u5316\u5185\u5b58\u7ba1\u7406:GIL \u907f\u514d\u4e86\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u5f15\u7528\u8ba1\u6570\u7684\u7ade\u4e89\u6761\u4ef6\uff0c\u7b80\u5316\u4e86CPython \u7684\u5185\u5b58\u7ba1\u7406\u3002\u63d0\u9ad8\u5355\u7ebf\u7a0b\u6027\u80fd:\u7531\u4e8e GIL \u7684\u5b58\u5728\uff0c\u5355\u7ebf\u7a0b\u7a0b\u5e8f\u7684\u6027\u80fd\u901a\u5e38\u66f4\u597d\uff0c\u56e0\u4e3a\u4e0d\u9700\u8981\u9891\u7e41\u5730\u83b7\u53d6\u548c\u91ca\u653e\u9501\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u7f3a\u70b9<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9650\u5236\u591a\u7ebf\u7a0b\u5e76\u884c:GIL\u5bfc\u81f4 Python \u7684\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u65e0\u6cd5\u5145\u5206\u5229\u7528\u591a\u6838 CPU \u7684\u5e76\u884c\u8ba1\u7b97\u80fd\u529b\u3002\u5f71\u54cd CPU \u5bc6\u96c6\u578b\u4efb\u52a1:\u5bf9\u4e8e CPU \u5bc6\u96c6\u578b\u4efb\u52a1(\u5982\u79d1\u5b66\u8ba1\u7b97\u3001\u56fe\u50cf\u5904\u7406)\uff0c\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u6027\u80fd\u53ef\u80fd\u4e0d\u5982\u5355\u7ebf\u7a0b\u7a0b\u5e8f\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u5982\u4f55\u7ed5\u8fc7GIL<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528\u591a\u8fdb\u7a0b:Python \u7684 multiprocessing \u6a21\u5757\u53ef\u4ee5\u521b\u5efa\u591a\u4e2a\u8fdb\u7a0b\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u6709\u72ec\u7acb\u7684Python \u89e3\u91ca\u5668\u548c\u5185\u5b58\u7a7a\u95f4\uff0c\u4ece\u800c\u5145\u5206\u5229\u7528\u591a\u6838 CPU\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 C \u6269\u5c55:\u5728 C \u6269\u5c55\u6a21\u5757\u4e2d\u91ca\u653e GIL\uff0c\u5141\u8bb8\u5176\u4ed6\u7ebf\u7a0b\u6267\u884c Python \u4ee3\u7801\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528\u5176\u4ed6 Python \u5b9e\u73b0:\u5982 Jython(\u57fa\u4e8e JVM)\u6216 lronPython(\u57fa\u4e8e .NET)\uff0c\u5b83\u4eec\u6ca1\u6709 GIL\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>import threading\ndef worker():\n    print(\"Worker thread\")   \nthreads = &#91;]\nfor i in range(5):\n    t= threading.Thread(target=worker)\n    threads.append(t)\n    t.start()\nfor t in threads:\n    t.join()    <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e03.Python\u4e2d\u7684_init \u548cnew \u6709\u4ec0\u4e48\u533a\u522b?<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>_init_ :\u662f\u5b9e\u4f8b\u521d\u59cb\u5316\u65b9\u6cd5\uff0c\u7528\u4e8e\u521d\u59cb\u5316\u5bf9\u8c61\u7684\u5c5e\u6027\u3002<\/li>\n\n\n\n<li>_new_ :\u662f\u5b9e\u4f8b\u521b\u5efa\u65b9\u6cd5\uff0c\u7528\u4e8e\u521b\u5efa\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5b9e\u4f8b\u3002\u5b83\u7684\u8c03\u7528\u53d1\u751f\u5728_init_() \u4e4b\u524d\u3002<\/li>\n\n\n\n<li>\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4f60\u4e0d\u9700\u8981\u91cd\u5199_new_()\u3002\u4f46\u5728\u4ee5\u4e0b\u573a\u666f\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u81ea\u5b9a\u4e49_new_():\n<ul class=\"wp-block-list\">\n<li><strong>\u5b9e\u73b0\u5355\u4f8b\u6a21\u5f0f<\/strong>:\u786e\u4fdd\u4e00\u4e2a\u7c7b\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\u3002<\/li>\n\n\n\n<li><strong>\u8fd4\u56de\u5b50\u7c7b\u5b9e\u4f8b<\/strong>:\u5728\u7236\u7c7b\u7684_new_()\u4e2d\u8fd4\u56de\u5b50\u7c7b\u7684\u5b9e\u4f8b\u3002<\/li>\n\n\n\n<li><strong>\u4fee\u6539\u5b9e\u4f8b\u521b\u5efa\u903b\u8f91<\/strong>:\u4f8b\u5982\uff0c\u6839\u636e\u6761\u4ef6\u8fd4\u56de\u4e0d\u540c\u7c7b\u578b\u7684\u5b9e\u4f8b<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>class MyClass:\n    def __new__(cls,*args,**kwargs):\n        print(\"Creating instance\")\n        instance =super(MyClass,cls).__new__(cls)\n        return instance\n    def __init__(self,value):\n        print(\"Initializing instance\")\n        self.value = value\nobj = MyClass(10)\n# \u8f93\u51fa:\n# Creating instance\n# Initializing instance<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u516b.Python\u4e2d\u7684lambda\u51fd\u6570\u662f\u4ec0\u4e48?\u5982\u4f55\u4f7f\u7528?<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>lambda\u51fd\u6570:\u662f\u4e00\u79cd\u533f\u540d\u51fd\u6570\uff0c\u901a\u5e38\u7528\u4e8e\u7b80\u5355\u7684\u64cd\u4f5c\u3002\u5b83\u53ef\u4ee5\u63a5\u53d7\u4efb\u610f\u6570\u91cf\u7684\u53c2\u6570\uff0c\u4f46\u53ea\u80fd\u6709\u4e00\u4e2a\u8868\u8fbe\u5f0f\u3002<\/li>\n\n\n\n<li>lambda \u51fd\u6570\u7684\u57fa\u672c\u8bed\u6cd5\n<ul class=\"wp-block-list\">\n<li>lambda \u662f\u5173\u952e\u5b57\uff0c\u8868\u793a\u5b9a\u4e49\u4e00\u4e2a\u533f\u540d\u51fd\u6570\u3002<\/li>\n\n\n\n<li>\u53c2\u65701, \u53c2\u65702,\u2026 \u662f\u51fd\u6570\u7684\u53c2\u6570\uff0c\u53ef\u4ee5\u6709\u591a\u4e2a\uff0c\u7528\u9017\u53f7\u5206\u9694\u3002<\/li>\n\n\n\n<li>\u8868\u8fbe\u5f0f \u662f\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff0clambda \u51fd\u6570\u4f1a\u8ba1\u7b97\u5e76\u8fd4\u56de\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u7684\u503c\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-cyan-bluish-gray-color\">lambda \u53c2\u65701\uff0c\u53c2\u65702\uff0c\u2026:\u8868\u8fbe\u5f0f<\/mark><\/strong><\/li>\n\n\n\n<li><\/li>\n\n\n\n<li>lambda \u51fd\u6570\u7684\u7279\u70b9\n<ul class=\"wp-block-list\">\n<li>\u533f\u540d\u6027:lambda \u51fd\u6570\u6ca1\u6709\u540d\u5b57\uff0c\u901a\u5e38\u7528\u4e8e\u4e00\u6b21\u6027\u4f7f\u7528\u7684\u573a\u666f<\/li>\n\n\n\n<li>\u7b80\u6d01\u6027:lambda \u51fd\u6570\u901a\u5e38\u53ea\u5305\u542b\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff0c\u4ee3\u7801\u7b80\u6d01\u3002<\/li>\n\n\n\n<li>\u5c40\u9650\u6027:lambda \u51fd\u6570\u4e0d\u80fd\u5305\u542b\u590d\u6742\u7684\u903b\u8f91(\u5982\u591a\u884c\u8bed\u53e5\u6216\u6761\u4ef6\u5224\u65ad)\uff0c\u53ea\u80fd\u8fd4\u56de\u4e00\u4e2a\u8868\u8fbe\u5f0f\u7684\u503c\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u793a\u4f8b1\nadd = lambda x,y:x+ y\nprint(add(2,3)) # \u8f93\u51fa:5\n# \u793a\u4f8b2\n#\u4f7f\u7528 sorted()\u5bf9\u5217\u8868\u4e2d\u7684\u5143\u7ec4\u6309\u7b2c\u4e8c\u4e2a\u5143\u7d20\u6392\u5e8f\npairs =&#91;(1,3),(2,1),(4,2)]\nsorted_pairs = sorted(pairs, key=lambda x: x&#91;1])\nprint(sorted_pairs) #\u8f93\u51fa:&#91;(2\uff0c1)\uff0c(4\uff0c2)\uff0c(1\uff0c3)]\n#\u793a\u4f8b3\nresult =&#91;\"apple\",\"banana\",\"cherry\",\"date\",\"fig\",\"grape\"]\nsorted_result =sorted(result, key=lambda x:(len(x),x))\n# lambda x:(len(x)\uff0cx)\u662f\u4e00\u4e2a\u533f\u540d\u51fd\u6570\uff0c\n#\u5b83\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570 x(\u5373\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20),\u5e76\u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4(len(x),x)# \n#\u6392\u5e8f\u89c4\u5219:1.\u9996\u5148\u6309\u957f\u5ea6\u6392\u5e8f:len(x)\u8868\u793a\u5143\u7d20\u7684\u957f\u5ea6\uff0c\u5217\u8868\u4f1a\u5148\u6309\u5143\u7d20\u7684\u957f\u5ea6\u4ece\u5c0f\u5230\u5927\u6392\u5e8f\u3002\n#2.\u957f\u5ea6\u76f8\u540c\u5219\u6309\u5b57\u5178\u5e8f\u6392\u5e8f:\u5982\u679c\u4e24\u4e2a\u5143\u7d20\u7684\u957f\u5ea6\u76f8\u540c\uff0c\u5219\u6309\u5143\u7d20\u672c\u8eab\u7684\u5b57\u5178\u5e8f(\u5373\u5b57\u6bcd\u6216\u6570\u5b57\u7684\u987a\u5e8f)\u6392\u5e8f\u3002\nprint(sorted_result)#\u8f93\u51fa:&#91;'fig','date','apple','grape''banana', 'cherry']<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e5d.Python\u4e2d\u7684map\u3001filter\u548creduce\u51fd\u6570\u662f\u4ec0\u4e48?\u5982\u4f55\u4f7f\u7528?<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">map<\/mark><\/strong>:\u5bf9\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u4e00\u4e2a\u51fd\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002\u3002\u8bed\u6cd5:<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-cyan-bluish-gray-color\">map(function,iterable\u2026.)<\/mark><\/strong><\/li>\n\n\n\n<li><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\"><strong>filter<\/strong><\/mark>:\u6839\u636e\u6761\u4ef6\u8fc7\u6ee4\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002<\/li>\n\n\n\n<li><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">reduce<\/mark><\/strong>:\u5bf9\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u7d2f\u79ef\u64cd\u4f5c\uff0c\u8fd4\u56de\u4e00\u4e2a\u5355\u4e00\u7684\u503c\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>from functools import reduce\nnumbers =&#91;1,2,3,4,5]\n# map\u793a\u4f8b\nsquared =map(lambda x:x** 2,numbers)\nprint(list(squared))#\u8f93\u51fa:&#91;1\uff0c4\uff0c9\uff0c16\uff0c25]\n# filter\u793a\u4f8b\nevens = filter(lambda x: x % 2==0,numbers)\nprint(list(evens))#\u8f93\u51fa:&#91;2\uff0c4]\n# reduce\u793a\u4f8b\ntotal =reduce(lambda x,y:x+y,numbers)\nprint(total)#\u8f93\u51fa:15<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5341.Python\u4e2d\u7684__slots__ \u662f\u4ec0\u4e48?\u5982\u4f55\u4f7f\u7528?<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>__slots__-:\u662f\u4e00\u4e2a\u7c7b\u53d8\u91cf\uff0c\u7528\u4e8e\u9650\u5236\u7c7b\u7684\u5b9e\u4f8b\u53ef\u4ee5\u62e5\u6709\u7684\u5c5e\u6027\u3002\u4f7f\u7528__slots__\u53ef\u4ee5\u8282\u7701\u5185\u5b58\uff0c\u56e0\u4e3a\u5b83\u963b\u6b62\u4e86\u5b9e\u4f8b\u5b57\u5178\u7684\u521b\u5efa\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>class MyClass:\n    __slots__=&#91;'name', 'age']\n    \n    def __init__ (self, name, age):\n        self.name = name\n        self.age = age\n        \nobj = MyClass(\"Alice\",25)\n# obj.address =\"123 Street'\n# \u8fd9\u884c\u4f1a\u62a5\u9519\uff0c\u56e0\u4e3aaddress\u4e0d\u5728_slots\u4e2d<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00.Python\u4e2d\u7684\u88c5\u9970\u5668\u662f\u4ec0\u4e48?\u5982\u4f55\u4f7f\u7528? \u88c5\u9970\u5668(Decorator):\u662f\u4e00\u79cd\u7528\u4e8e\u4fee\u6539\u6216\u6269\u5c55\u51fd\u6570\u6216\u65b9\u6cd5\u884c\u4e3a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":251,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3,1],"tags":[],"class_list":["post-248","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-1"],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.linerroom.cn\/wp-content\/uploads\/2025\/11\/0-13.jpg?fit=1696%2C2445&ssl=1","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=\/wp\/v2\/posts\/248","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=248"}],"version-history":[{"count":3,"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=\/wp\/v2\/posts\/248\/revisions"}],"predecessor-version":[{"id":257,"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=\/wp\/v2\/posts\/248\/revisions\/257"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=\/wp\/v2\/media\/251"}],"wp:attachment":[{"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}