{"id":6085,"date":"2022-08-23T19:22:20","date_gmt":"2022-08-23T11:22:20","guid":{"rendered":"http:\/\/139.9.1.231\/?p=6085"},"modified":"2022-08-23T20:33:23","modified_gmt":"2022-08-23T12:33:23","slug":"python-4","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/08\/23\/python-4\/","title":{"rendered":"Python\u88c5\u9970\u5668\uff1apython\u4e2d\u7684@\u7b26\u53f7\u7684\u4f5c\u7528 \u4ee5\u53ca torch\u4e2d\u7ecf\u5e38\u51fa\u73b0\u7684 @torch.no_grad()"},"content":{"rendered":"\n<p>@\u7b26\u53f7\u662f\u88c5\u9970\u5668\uff08\u4fee\u9970\u7b26\uff09\u7684\u8bed\u6cd5\u7cd6\uff0c\u5728\u5b9a\u4e49\u51fd\u6570\u7684\u65f6\u5019\u4f7f\u7528\uff0c\u907f\u514d\u518d\u4e00\u6b21\u8d4b\u503c\u64cd\u4f5c<\/p>\n\n\n\n<p>\u88c5\u9970\u5668(Decorators)\u662f Python \u7684\u4e00\u4e2a\u91cd\u8981\u90e8\u5206\u3002\u7b80\u5355\u5730\u8bf4\uff1a\u4ed6\u4eec\u662f\u4fee\u6539\u5176\u4ed6\u51fd\u6570\u7684\u529f\u80fd\u7684\u51fd\u6570\u3002\u4ed6\u4eec\u6709\u52a9\u4e8e\u8ba9\u6211\u4eec\u7684\u4ee3\u7801\u66f4\u7b80\u77ed\uff0c\u4e5f\u66f4Pythonic\uff08Python\u8303\u513f\uff09\u3002\u5927\u591a\u6570\u521d\u5b66\u8005\u4e0d\u77e5\u9053\u5728\u54ea\u513f\u4f7f\u7528\u5b83\u4eec\uff0c\u6240\u4ee5\u6211\u5c06\u8981\u5206\u4eab\u4e0b\uff0c\u54ea\u4e9b\u533a\u57df\u91cc\u88c5\u9970\u5668\u53ef\u4ee5\u8ba9\u4f60\u7684\u4ee3\u7801\u66f4\u7b80\u6d01\u3002 \u9996\u5148\uff0c\u8ba9\u6211\u4eec\u8ba8\u8bba\u4e0b\u5982\u4f55\u5199\u4f60\u81ea\u5df1\u7684\u88c5\u9970\u5668\u3002<\/p>\n\n\n\n<p>       <strong>&#8216;@&#8217;\u7b26\u53f7\u7528\u4f5c\u51fd\u6570\u4fee\u9970\u7b26\u662fpython2.4\u65b0\u589e\u52a0\u7684\u529f\u80fd\uff0c\u4fee\u9970\u7b26\u5fc5\u987b\u51fa\u73b0\u5728\u51fd\u6570\u5b9a\u4e49\u524d\u4e00\u884c\uff0c\u4e0d\u5141\u8bb8\u548c\u51fd\u6570\u5b9a\u4e49\u5728\u540c\u4e00\u884c\u3002\u4e5f\u5c31\u662f\u8bf4\uff20A def f(): \u662f\u975e\u6cd5\u7684\u3002\u53ea\u53ef\u4ee5\u5728\u6a21\u5757\u6216\u7c7b\u5b9a\u4e49\u5c42\u5185\u5bf9\u51fd\u6570\u8fdb\u884c\u4fee\u9970\uff0c\u4e0d\u5141\u8bb8\u4fee\u9970\u4e00\u4e2a\u7c7b\u3002\u4e00\u4e2a\u4fee\u9970\u7b26\u5c31\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u5b83\u5c06\u88ab\u4fee\u9970\u7684\u51fd\u6570\u505a\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4fee\u9970\u540e\u7684\u540c\u540d\u51fd\u6570\u6216\u5176\u5b83\u53ef\u8c03\u7528\u7684\u4e1c\u897f\u3002<\/strong><\/p>\n\n\n\n<p><strong>\u5b9e\u4f8b\uff081\uff09\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def spamrun(fn):\n   def sayspam(*args):\n       print(\"spam,spam,spam\")\n   return sayspam<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>@spamrun\ndef useful(a,b):\n   print (a**2+b**2)<\/code><\/pre>\n\n\n\n<p>\u6267\u884c\uff1a useful(3,4)<\/p>\n\n\n\n<p>\u8fd4\u56de\uff1aspam,spam,spam<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def addspam(fn):\n   def new(*args):\n       print \"spam,spam,spam\"\n       return fn(*args)\n   return new\n\n@addspam\ndef useful(a,b):\n   print a**2+b**2<\/code><\/pre>\n\n\n\n<p>\u6267\u884c\uff1a useful(4,3)<\/p>\n\n\n\n<p><strong>\u7ed3\u679c\uff1a<\/strong><\/p>\n\n\n\n<p>spam,spam,spam<\/p>\n\n\n\n<p>25<\/p>\n\n\n\n<h2>@torch.no_grad()<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>@torch.no_grad()\ndef eval():\n\t...<\/code><\/pre>\n\n\n\n<p><code>@torch.no_grad()<\/code>\u540e\u9762\u7684\u51fd\u6570\u7684\u6570\u636e\u4e0d\u9700\u8981\u8ba1\u7b97\u68af\u5ea6\uff0c\u4e5f\u4e0d\u4f1a\u8fdb\u884c\u53cd\u5411\u4f20\u64ad<\/p>\n\n\n\n<h2>Python\u88c5\u9970\u5668\uff1a<\/h2>\n\n\n\n<p>\u88c5\u9970\u5668\u672c\u8d28\u4e0a\u662f\u4e00\u4e2aPython\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u8ba9\u5176\u4ed6\u51fd\u6570\u5728\u4e0d\u9700\u8981\u505a\u4efb\u4f55\u4ee3\u7801\u53d8\u52a8\u7684\u524d\u63d0\u4e0b\u589e\u52a0\u989d\u5916\u529f\u80fd\uff0c\u88c5\u9970\u5668\u7684\u8fd4\u56de\u503c\u4e5f\u662f\u4e00\u4e2a\u51fd\u6570\u5bf9\u8c61\u3002\u5b83\u7ecf\u5e38\u7528\u4e8e\u6709\u5207\u9762\u9700\u6c42\u7684\u573a\u666f\uff0c\u6bd4\u5982\uff1a\u63d2\u5165\u65e5\u5fd7\u3001\u6027\u80fd\u6d4b\u8bd5\u3001\u4e8b\u52a1\u5904\u7406\u3001\u7f13\u5b58\u3001\u6743\u9650\u6821\u9a8c\u7b49\u573a\u666f\u3002\u88c5\u9970\u5668\u662f\u89e3\u51b3\u8fd9\u7c7b\u95ee\u9898\u7684\u7edd\u4f73\u8bbe\u8ba1\uff0c\u6709\u4e86\u88c5\u9970\u5668\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u62bd\u79bb\u51fa\u5927\u91cf\u4e0e\u51fd\u6570\u529f\u80fd\u672c\u8eab\u65e0\u5173\u7684\u96f7\u540c\u4ee3\u7801\u5e76\u7ee7\u7eed\u91cd\u7528\u3002\u6982\u62ec\u7684\u8bb2\uff0c\u88c5\u9970\u5668\u7684\u4f5c\u7528\u5c31\u662f\u4e3a\u5df2\u7ecf\u5b58\u5728\u7684\u5bf9\u8c61\u6dfb\u52a0\u989d\u5916\u7684\u529f\u80fd\u3002<\/p>\n\n\n\n<p>\u5148\u6765\u770b\u4e00\u4e2a\u7b80\u5355\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def foo():\n    print('i am foo')\n<\/code><\/pre>\n\n\n\n<p>\u73b0\u5728\u6709\u4e00\u4e2a\u65b0\u7684\u9700\u6c42\uff0c\u5e0c\u671b\u53ef\u4ee5\u8bb0\u5f55\u4e0b\u51fd\u6570\u7684\u6267\u884c\u65e5\u5fd7\uff0c\u4e8e\u662f\u5728\u4ee3\u7801\u4e2d\u6dfb\u52a0\u65e5\u5fd7\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def foo():\n    print('i am foo')\n    logging.info(\"foo is running\")\n<\/code><\/pre>\n\n\n\n<p>bar()\u3001bar2()\u4e5f\u6709\u7c7b\u4f3c\u7684\u9700\u6c42\uff0c\u600e\u4e48\u505a\uff1f\u518d\u5199\u4e00\u4e2alogging\u5728bar\u51fd\u6570\u91cc\uff1f\u8fd9\u6837\u5c31\u9020\u6210\u5927\u91cf\u96f7\u540c\u7684\u4ee3\u7801\uff0c\u4e3a\u4e86\u51cf\u5c11\u91cd\u590d\u5199\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u505a\uff0c\u91cd\u65b0\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff1a\u4e13\u95e8\u5904\u7406\u65e5\u5fd7 \uff0c\u65e5\u5fd7\u5904\u7406\u5b8c\u4e4b\u540e\u518d\u6267\u884c\u771f\u6b63\u7684\u4e1a\u52a1\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def use_logging(func):\n    logging.warn(\"%s is running\" % func.__name__)\n    func()\n\ndef bar():\n    print('i am bar')\n\nuse_logging(bar)\n<\/code><\/pre>\n\n\n\n<p>\u903b\u8f91\u4e0a\u4e0d\u96be\u7406\u89e3\uff0c \u4f46\u662f\u8fd9\u6837\u7684\u8bdd\uff0c\u6211\u4eec\u6bcf\u6b21\u90fd\u8981\u5c06\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9use_logging\u51fd\u6570\u3002\u800c\u4e14\u8fd9\u79cd\u65b9\u5f0f\u5df2\u7ecf\u7834\u574f\u4e86\u539f\u6709\u7684\u4ee3\u7801\u903b\u8f91\u7ed3\u6784\uff0c\u4e4b\u524d\u6267\u884c\u4e1a\u52a1\u903b\u8f91\u65f6\uff0c\u6267\u884c\u8fd0\u884cbar()\uff0c\u4f46\u662f\u73b0\u5728\u4e0d\u5f97\u4e0d\u6539\u6210use_logging(bar)\u3002\u90a3\u4e48\u6709\u6ca1\u6709\u66f4\u597d\u7684\u65b9\u5f0f\u7684\u5462\uff1f\u5f53\u7136\u6709\uff0c\u7b54\u6848\u5c31\u662f\u88c5\u9970\u5668\u3002<\/p>\n\n\n\n<p><strong>\u7b80\u5355\u88c5\u9970\u5668<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def use_logging(func):\n\n    def wrapper(*args, **kwargs):\n        logging.warn(\"%s is running\" % func.__name__)\n        return func(*args, **kwargs)\n    return wrapper\n\ndef bar():\n    print('i am bar')\n\nbar = use_logging(bar)\nbar()\n<\/code><\/pre>\n\n\n\n<p>\u51fd\u6570use_logging\u5c31\u662f\u88c5\u9970\u5668\uff0c\u5b83\u628a\u6267\u884c\u771f\u6b63\u4e1a\u52a1\u65b9\u6cd5\u7684func\u5305\u88f9\u5728\u51fd\u6570\u91cc\u9762\uff0c\u770b\u8d77\u6765\u50cfbar\u88abuse_logging\u88c5\u9970\u4e86\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u51fd\u6570\u8fdb\u5165\u548c\u9000\u51fa\u65f6 \uff0c\u88ab\u79f0\u4e3a\u4e00\u4e2a\u6a2a\u5207\u9762(Aspect)\uff0c\u8fd9\u79cd\u7f16\u7a0b\u65b9\u5f0f\u88ab\u79f0\u4e3a\u9762\u5411\u5207\u9762\u7684\u7f16\u7a0b(Aspect-Oriented Programming)\u3002<\/p>\n\n\n\n<p>@\u7b26\u53f7\u662f\u88c5\u9970\u5668\u7684\u8bed\u6cd5\u7cd6\uff0c\u5728\u5b9a\u4e49\u51fd\u6570\u7684\u65f6\u5019\u4f7f\u7528\uff0c\u907f\u514d\u518d\u4e00\u6b21\u8d4b\u503c\u64cd\u4f5c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u65b9\u6cd5\u4e00\uff1a\u4e0d\u7528\u8bed\u6cd5\u7cd6@\u7b26\u53f7\u200b\u200b\u200b\u200b\u200b\u200b\u200b\r\n\r\n# \u88c5\u9970\u5668\u4e0d\u4f20\u5165\u53c2\u6570\u65f6\r\nf = decorator(\u51fd\u6570\u540d)\r\n\r\n# \u88c5\u9970\u5668\u4f20\u5165\u53c2\u6570\u65f6\r\nf = (decorator(\u53c2\u6570))(\u51fd\u6570\u540d)\r\n\r\n\r\n\u65b9\u6cd5\u4e8c\uff1a\u91c7\u7528\u8bed\u6cd5\u7cd6@\u7b26\u53f7\u200b\u200b\u200b\u200b\u200b\u200b\u200b\r\n\r\n# \u5df2\u5b9a\u4e49\u7684\u88c5\u9970\u5668\r\n@decorator \r\ndef f():  \r\n    pass\r\n\r\n# \u6267\u884c\u88ab\u88c5\u9970\u8fc7\u7684\u51fd\u6570 \r\nf()<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>def use_logging(func):\n\n    def wrapper(*args, **kwargs):\n        logging.warn(\"%s is running\" % func.__name__)\n        return func(*args)\n    return wrapper\n\n@use_logging\ndef foo():\n    print(\"i am foo\")\n\n@use_logging\ndef bar():\n    print(\"i am bar\")\n\nbar()\n<\/code><\/pre>\n\n\n\n<p>\u5982\u4e0a\u6240\u793a\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u7701\u53bbbar = use_logging(bar)\u8fd9\u4e00\u53e5\u4e86\uff0c\u76f4\u63a5\u8c03\u7528bar()\u5373\u53ef\u5f97\u5230\u60f3\u8981\u7684\u7ed3\u679c\u3002\u5982\u679c\u6211\u4eec\u6709\u5176\u4ed6\u7684\u7c7b\u4f3c\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u7ee7\u7eed\u8c03\u7528\u88c5\u9970\u5668\u6765\u4fee\u9970\u51fd\u6570\uff0c\u800c\u4e0d\u7528\u91cd\u590d\u4fee\u6539\u51fd\u6570\u6216\u8005\u589e\u52a0\u65b0\u7684\u5c01\u88c5\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u63d0\u9ad8\u4e86\u7a0b\u5e8f\u7684\u53ef\u91cd\u590d\u5229\u7528\u6027\uff0c\u5e76\u589e\u52a0\u4e86\u7a0b\u5e8f\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n\n\n\n<p>\u88c5\u9970\u5668\u5728Python\u4f7f\u7528\u5982\u6b64\u65b9\u4fbf\u90fd\u8981\u5f52\u56e0\u4e8ePython\u7684\u51fd\u6570\u80fd\u50cf\u666e\u901a\u7684\u5bf9\u8c61\u4e00\u6837\u80fd\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u5176\u4ed6\u51fd\u6570\uff0c\u53ef\u4ee5\u88ab\u8d4b\u503c\u7ed9\u5176\u4ed6\u53d8\u91cf\uff0c\u53ef\u4ee5\u4f5c\u4e3a\u8fd4\u56de\u503c\uff0c\u53ef\u4ee5\u88ab\u5b9a\u4e49\u5728\u53e6\u5916\u4e00\u4e2a\u51fd\u6570\u5185\u3002<\/p>\n\n\n\n<p><strong>\u5e26\u53c2\u6570\u7684\u88c5\u9970\u5668<\/strong><\/p>\n\n\n\n<p>\u88c5\u9970\u5668\u8fd8\u6709\u66f4\u5927\u7684\u7075\u6d3b\u6027\uff0c\u4f8b\u5982\u5e26\u53c2\u6570\u7684\u88c5\u9970\u5668\uff1a\u5728\u4e0a\u9762\u7684\u88c5\u9970\u5668\u8c03\u7528\u4e2d\uff0c\u6bd4\u5982@use_logging\uff0c\u8be5\u88c5\u9970\u5668\u552f\u4e00\u7684\u53c2\u6570\u5c31\u662f\u6267\u884c\u4e1a\u52a1\u7684\u51fd\u6570\u3002\u88c5\u9970\u5668\u7684\u8bed\u6cd5\u5141\u8bb8\u6211\u4eec\u5728\u8c03\u7528\u65f6\uff0c\u63d0\u4f9b\u5176\u5b83\u53c2\u6570\uff0c\u6bd4\u5982@decorator(a)\u3002\u8fd9\u6837\uff0c\u5c31\u4e3a\u88c5\u9970\u5668\u7684\u7f16\u5199\u548c\u4f7f\u7528\u63d0\u4f9b\u4e86\u66f4\u5927\u7684\u7075\u6d3b\u6027\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def use_logging(level):\n    def decorator(func):\n        def wrapper(*args, **kwargs):\n            if level == \"warn\":\n                logging.warn(\"%s is running\" % func.__name__)\n            return func(*args)\n        return wrapper\n\n    return decorator\n\n@use_logging(level=\"warn\")\ndef foo(name='foo'):\n    print(\"i am %s\" % name)\n\nfoo()\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684use_logging\u662f\u5141\u8bb8\u5e26\u53c2\u6570\u7684\u88c5\u9970\u5668\u3002\u5b83\u5b9e\u9645\u4e0a\u662f\u5bf9\u539f\u6709\u88c5\u9970\u5668\u7684\u4e00\u4e2a\u51fd\u6570\u5c01\u88c5\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u88c5\u9970\u5668\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u5b83\u7406\u89e3\u4e3a\u4e00\u4e2a\u542b\u6709\u53c2\u6570\u7684\u95ed\u5305\u3002\u5f53\u6211 \u4eec\u4f7f\u7528@use_logging(level=&#8221;warn&#8221;)\u8c03\u7528\u7684\u65f6\u5019\uff0cPython\u80fd\u591f\u53d1\u73b0\u8fd9\u4e00\u5c42\u7684\u5c01\u88c5\uff0c\u5e76\u628a\u53c2\u6570\u4f20\u9012\u5230\u88c5\u9970\u5668\u7684\u73af\u5883\u4e2d\u3002<\/p>\n\n\n\n<p><strong>\u7c7b\u88c5\u9970\u5668<\/strong><\/p>\n\n\n\n<p>\u518d\u6765\u770b\u770b\u7c7b\u88c5\u9970\u5668\uff0c\u76f8\u6bd4\u51fd\u6570\u88c5\u9970\u5668\uff0c\u7c7b\u88c5\u9970\u5668\u5177\u6709\u7075\u6d3b\u5ea6\u5927\u3001\u9ad8\u5185\u805a\u3001\u5c01\u88c5\u6027\u7b49\u4f18\u70b9\u3002\u4f7f\u7528\u7c7b\u88c5\u9970\u5668\u8fd8\u53ef\u4ee5\u4f9d\u9760\u7c7b\u5185\u90e8\u7684__call__\u65b9\u6cd5\uff0c\u5f53\u4f7f\u7528 @ \u5f62\u5f0f\u5c06\u88c5\u9970\u5668\u9644\u52a0\u5230\u51fd\u6570\u4e0a\u65f6\uff0c\u5c31\u4f1a\u8c03\u7528\u6b64\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p> __call__\u65b9\u6cd5 : \u5728\u751f\u6210\u4e00\u4e2a\u7c7b\u7684\u5b9e\u4f8b\u65f6\uff0c\u81ea\u52a8\u81ea\u884c\u4e00\u6b21call\u65b9\u6cd5<\/p>\n\n\n\n<p>\u5f53\u6267\u884cFoo\u65f6\u5019\u751f\u6210\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5c31\u4f1a\u81ea\u52a8\u8c03\u7528__call__\u65b9\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Foo(object):\n    def __init__(self, func):\n    self._func = func\n\ndef __call__(self):\n    print ('class decorator runing')\n    self._func()\n    print ('class decorator ending')\n\n@Foo\ndef bar():\n    print ('bar')\n\nbar()<\/code><\/pre>\n\n\n\n<p><strong>functools.wraps<\/strong><\/p>\n\n\n\n<p>\u4f7f\u7528\u88c5\u9970\u5668\u6781\u5927\u5730\u590d\u7528\u4e86\u4ee3\u7801\uff0c\u4f46\u662f\u4ed6\u6709\u4e00\u4e2a\u7f3a\u70b9\u5c31\u662f\u539f\u51fd\u6570\u7684\u5143\u4fe1\u606f\u4e0d\u89c1\u4e86\uff0c\u6bd4\u5982\u51fd\u6570\u7684docstring\u3001__name__\u3001\u53c2\u6570\u5217\u8868\uff0c\u5148\u770b\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<p>\u88c5\u9970\u5668<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def logged(func):\n    def with_logging(*args, **kwargs):\n        print func.__name__ + \" was called\"\n        return func(*args, **kwargs)\n    return with_logging\n<\/code><\/pre>\n\n\n\n<p>\u51fd\u6570<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@logged\ndef f(x):\n   \"\"\"does some math\"\"\"\n   return x + x * x\n<\/code><\/pre>\n\n\n\n<p>\u8be5\u51fd\u6570\u5b8c\u6210\u7b49\u4ef7\u4e8e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def f(x):\n    \"\"\"does some math\"\"\"\n    return x + x * x\nf = logged(f)\n<\/code><\/pre>\n\n\n\n<p>\u4e0d\u96be\u53d1\u73b0\uff0c\u51fd\u6570f\u88abwith_logging\u53d6\u4ee3\u4e86\uff0c\u5f53\u7136\u5b83\u7684docstring\uff0c__name__\u5c31\u662f\u53d8\u6210\u4e86with_logging\u51fd\u6570\u7684\u4fe1\u606f\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print f.__name__    <em># prints 'with_logging'<\/em>\nprint f.__doc__     <em># prints None<\/em>\n<\/code><\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u95ee\u9898\u5c31\u6bd4\u8f83\u4e25\u91cd\u7684\uff0c\u597d\u5728\u6211\u4eec\u6709functools.wraps\uff0cwraps\u672c\u8eab\u4e5f\u662f\u4e00\u4e2a\u88c5\u9970\u5668\uff0c\u5b83\u80fd\u628a\u539f\u51fd\u6570\u7684\u5143\u4fe1\u606f\u62f7\u8d1d\u5230\u88c5\u9970\u5668\u51fd\u6570\u4e2d\uff0c\u8fd9\u4f7f\u5f97\u88c5\u9970\u5668\u51fd\u6570\u4e5f\u6709\u548c\u539f\u51fd\u6570\u4e00\u6837\u7684\u5143\u4fe1\u606f\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from functools import wraps\ndef logged(func):\n    @wraps(func)\n    def with_logging(*args, **kwargs):\n        print func.__name__ + \" was called\"\n        return func(*args, **kwargs)\n    return with_logging\n\n@logged\ndef f(x):\n    \"\"\"does some math\"\"\"\n    return x + x * x\n\nprint f.__name__  <em># prints 'f'<\/em>\nprint f.__doc__   <em># prints 'does some math'<\/em>\n<\/code><\/pre>\n\n\n\n<p><strong>\u5185\u7f6e\u88c5\u9970\u5668<\/strong><\/p>\n\n\n\n<p>@staticmathod\u3001@classmethod\u3001@property<\/p>\n\n\n\n<p><strong>@property<\/strong><\/p>\n\n\n\n<p>\u628a\u7c7b\u5185\u65b9\u6cd5\u5f53\u6210\u5c5e\u6027\u6765\u4f7f\u7528\uff0c\u5fc5\u987b\u8981\u6709\u8fd4\u56de\u503c\uff0c\u76f8\u5f53\u4e8egetter\uff1b<\/p>\n\n\n\n<p>\u5047\u5982\u6ca1\u6709\u5b9a\u4e49 @func.setter \u4fee\u9970\u65b9\u6cd5\u7684\u8bdd\uff0c\u5c31\u662f\u53ea\u8bfb\u5c5e\u6027<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Car:\n\n    def __init__(self, name, price):\n        self._name = name\n        self._price = price    \n     \n    @property\n    def car_name(self):\n        return self._name\n        \n     # car_name\u53ef\u4ee5\u8bfb\u5199\u7684\u5c5e\u6027   \n     @car_name.setter\n     def car_name(self, value):\n         self._name = value\n         \n     # car_price\u662f\u53ea\u8bfb\u5c5e\u6027 \n     @property\n     def car_price(self):\n         return str(self._price) + '\u4e07'\n         \nbenz = Car('benz', 30)\n\nprint(benz.car_name)   # benz\nbenz.car_name = \"baojun\"\nprint(benz.car_name)   # baojun\nprint(benz.car_price)  # 30\u4e07<\/code><\/pre>\n\n\n\n<p><strong><code>@staticmethod<\/code><\/strong><\/p>\n\n\n\n<p><strong>\u9759\u6001\u65b9\u6cd5<\/strong>\uff0c\u4e0d\u9700\u8981\u8868\u793a\u81ea\u8eab\u5bf9\u8c61\u7684self\u548c\u81ea\u8eab\u7c7b\u7684cls\u53c2\u6570\uff0c\u5c31\u8ddf\u4f7f\u7528\u51fd\u6570\u4e00\u6837\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u9759\u6001\u65b9\u6cd5\u7684\u4f7f\u7528\u573a\u666f\uff1a\n\n\u5982\u679c\u5728\u65b9\u6cd5\u4e2d\u4e0d\u9700\u8981\u8bbf\u95ee\u4efb\u4f55\u5b9e\u4f8b\u65b9\u6cd5\u548c\u5c5e\u6027\uff0c\u7eaf\u7cb9\u5730\u901a\u8fc7\u4f20\u5165\u53c2\u6570\u5e76\u8fd4\u56de\u6570\u636e\u7684\u529f\u80fd\u6027\u65b9\u6cd5\uff0c\u90a3\u4e48\u5b83\u5c31\u9002\u5408\u7528\u9759\u6001\u65b9\u6cd5\u6765\u5b9a\u4e49\uff0c\u5b83\u8282\u7701\u4e86\u5b9e\u4f8b\u5316\u5bf9\u8c61\u7684\u5f00\u9500\u6210\u672c\uff0c\u5f80\u5f80\u8fd9\u79cd\u65b9\u6cd5\u653e\u5728\u7c7b\u5916\u9762\u7684\u6a21\u5757\u5c42\u4f5c\u4e3a\u4e00\u4e2a\u51fd\u6570\u5b58\u5728\u4e5f\u662f\u6ca1\u95ee\u9898\u7684\uff0c\u800c\u653e\u5728\u7c7b\u4e2d\uff0c\u4ec5\u4e3a\u8fd9\u4e2a\u7c7b\u670d\u52a1\u3002<\/code><\/pre>\n\n\n\n<p><strong><code>@classmethod<\/code><\/strong><\/p>\n\n\n\n<p><strong>\u7c7b\u65b9\u6cd5<\/strong>\uff0c\u4e0d\u9700\u8981self\u53c2\u6570\uff0c\u4f46\u7b2c\u4e00\u4e2a\u53c2\u6570\u9700\u8981\u662f\u8868\u793a\u81ea\u8eab\u7c7b\u7684cls\u53c2\u6570\u3002<\/p>\n\n\n\n<p>\u7c7b\u65b9\u6cd5\u7684\u4f7f\u7528\u573a\u666f\u6709\uff1a<\/p>\n\n\n\n<p>\u4f5c\u4e3a\u5de5\u5382\u65b9\u6cd5\u521b\u5efa\u5b9e\u4f8b\u5bf9\u8c61\uff0c\u4f8b\u5982\u5185\u7f6e\u6a21\u5757 datetime.date \u7c7b\u4e2d\u5c31\u6709\u5927\u91cf\u4f7f\u7528\u7c7b\u65b9\u6cd5\u4f5c\u4e3a\u5de5\u5382\u65b9\u6cd5\uff0c\u4ee5\u6b64\u6765\u521b\u5efadate\u5bf9\u8c61\u3002\u5982\u679c\u5e0c\u671b\u5728\u65b9\u6cd5\u91cc\u9762\u8c03\u7528\u9759\u6001\u7c7b\uff0c\u90a3\u4e48\u628a\u65b9\u6cd5\u5b9a\u4e49\u6210\u7c7b\u65b9\u6cd5\u662f\u5408\u9002\u7684\uff0c\u56e0\u4e3a\u8981\u662f\u5b9a\u4e49\u6210\u9759\u6001\u65b9\u6cd5\uff0c\u90a3\u4e48\u4f60\u5c31\u8981\u663e\u793a\u5730\u5f15\u7528\u7c7bA\uff0c\u8fd9\u5bf9\u7ee7\u627f\u6765\u8bf4\u53ef\u4e0d\u662f\u4e00\u4ef6\u597d\u4e8b\u60c5\u3002<\/p>\n\n\n\n<p>\u4f8b\u5b50<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Demo(object):\n\n    text = \"\u4e09\u79cd\u65b9\u6cd5\u7684\u6bd4\u8f83\"\n    \n    def instance_method(self):\n        print(\"\u8c03\u7528\u5b9e\u4f8b\u65b9\u6cd5\")\n\n    @classmethod\n    def class_method(cls):\n        print(\"\u8c03\u7528\u7c7b\u65b9\u6cd5\")\n        print(\"\u5728\u7c7b\u65b9\u6cd5\u4e2d \u8bbf\u95ee\u7c7b\u5c5e\u6027 text: {}\".format(cls.text))\n        print(\"\u5728\u7c7b\u65b9\u6cd5\u4e2d \u8c03\u7528\u5b9e\u4f8b\u65b9\u6cd5 instance_method: {}\".format(cls().instance_method()))\n\n    @staticmethod\n    def static_method():\n        print(\"\u8c03\u7528\u9759\u6001\u65b9\u6cd5\")\n        print(\"\u5728\u9759\u6001\u65b9\u6cd5\u4e2d \u8bbf\u95ee\u7c7b\u5c5e\u6027 text: {}\".format(Demo.text))\n        print(\"\u5728\u9759\u6001\u65b9\u6cd5\u4e2d \u8c03\u7528\u5b9e\u4f8b\u65b9\u6cd5 instance_method: {}\".format(Demo().instance_method()))\n\nif __name__ == \"__main__\":\n    # \u5b9e\u4f8b\u5316\u5bf9\u8c61\n    d = Demo()\n    \n    # \u5bf9\u8c61\u53ef\u4ee5\u8bbf\u95ee \u5b9e\u4f8b\u65b9\u6cd5\u3001\u7c7b\u65b9\u6cd5\u3001\u9759\u6001\u65b9\u6cd5\n    # \u901a\u8fc7\u5bf9\u8c61\u8bbf\u95eetext\u5c5e\u6027\n    print(d.text)\n    \n    # \u901a\u8fc7\u5bf9\u8c61\u8c03\u7528\u5b9e\u4f8b\u65b9\u6cd5\n    d.instance_method()\n    \n    # \u901a\u8fc7\u5bf9\u8c61\u8c03\u7528\u7c7b\u65b9\u6cd5\n    d.class_method()\n    \n    # \u901a\u8fc7\u5bf9\u8c61\u8c03\u7528\u9759\u6001\u65b9\u6cd5\n    d.static_method()\n    \n    # \u7c7b\u53ef\u4ee5\u8bbf\u95ee\u7c7b\u65b9\u6cd5\u3001\u9759\u6001\u65b9\u6cd5\n    # \u901a\u8fc7\u7c7b\u8bbf\u95eetext\u5c5e\u6027\n    print(Demo.text)\n    \n    # \u901a\u8fc7\u7c7b\u8c03\u7528\u7c7b\u65b9\u6cd5\n    Demo.class_method()\n    \n    # \u901a\u8fc7\u7c7b\u8c03\u7528\u9759\u6001\u65b9\u6cd5\n    Demo.static_method()<\/code><\/pre>\n\n\n\n<p><strong>@staticmethod<\/strong>&nbsp;\u548c&nbsp;<strong>@classmethod<\/strong>&nbsp;\u7684&nbsp;<strong>\u533a\u522b<\/strong>&nbsp;\u548c&nbsp;<strong>\u4f7f\u7528\u573a\u666f<\/strong>\uff1a<\/p>\n\n\n\n<p>\u5728\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u51fa\uff0c<\/p>\n\n\n\n<p><strong>\u533a\u522b<\/strong><\/p>\n\n\n\n<p>\u5728\u5b9a\u4e49\u9759\u6001\u7c7b\u65b9\u6cd5\u548c\u7c7b\u65b9\u6cd5\u65f6\uff0c<strong>@staticmethod<\/strong>&nbsp;\u88c5\u9970\u7684\u9759\u6001\u65b9\u6cd5\u91cc\u9762\uff0c\u60f3\u8981\u8bbf\u95ee\u7c7b\u5c5e\u6027\u6216\u8c03\u7528\u5b9e\u4f8b\u65b9\u6cd5\uff0c\u5fc5\u987b\u9700\u8981\u628a\u7c7b\u540d\u5199\u4e0a\uff1b<\/p>\n\n\n\n<p>\u800c<strong>@classmethod<\/strong>\u88c5\u9970\u7684\u7c7b\u65b9\u6cd5\u91cc\u9762\uff0c\u4f1a\u4f20\u4e00\u4e2acls\u53c2\u6570\uff0c\u4ee3\u8868\u672c\u7c7b\uff0c\u8fd9\u6837\u5c31\u80fd\u591f\u907f\u514d\u624b\u5199\u7c7b\u540d\u7684\u786c\u7f16\u7801\u3002<\/p>\n\n\n\n<p>\u5728\u8c03\u7528\u9759\u6001\u65b9\u6cd5\u548c\u7c7b\u65b9\u6cd5\u65f6\uff0c\u5b9e\u9645\u4e0a\u5199\u6cd5\u90fd\u5dee\u4e0d\u591a\uff0c\u4e00\u822c\u90fd\u662f\u901a\u8fc7 \u7c7b\u540d.\u9759\u6001\u65b9\u6cd5() \u6216 \u7c7b\u540d.\u7c7b\u65b9\u6cd5()\u3002\u4e5f\u53ef\u4ee5\u7528\u5b9e\u4f8b\u5bf9\u8c61\u8c03\u7528\u7c7b\u65b9\u6cd5\u548c\u9759\u6001\u65b9\u6cd5\u3002 \u5bf9\u8c61\u53ef\u4ee5\u8bbf\u95ee \u5b9e\u4f8b\u65b9\u6cd5\u3001\u7c7b\u65b9\u6cd5\u3001\u9759\u6001\u65b9\u6cd5 \uff0c \u7c7b\u53ef\u4ee5\u8bbf\u95ee\u7c7b\u65b9\u6cd5\u3001\u9759\u6001\u65b9\u6cd5 <\/p>\n\n\n\n<p><strong>\u4e5f\u53ef\u4ee5\u7528\u5b9e\u4f8b\u5316\u5bf9\u8c61\u53bb\u8c03\u7528\u9759\u6001\u65b9\u6cd5\u548c\u7c7b\u65b9\u6cd5<\/strong>\uff0c<strong>\u4f46\u4e3a\u4e86\u548c\u5b9e\u4f8b\u65b9\u6cd5\u533a\u5206\uff0c\u6700\u597d\u8fd8\u662f\u7528\u7c7b\u53bb\u8c03\u7528\u9759\u6001\u65b9\u6cd5\u548c\u7c7b\u65b9\u6cd5\u3002<\/strong><\/p>\n\n\n\n<p><strong>\u4f7f\u7528\u573a\u666f<\/strong><\/p>\n\n\n\n<p>\u6240\u4ee5\uff0c\u5728\u5b9a\u4e49\u7c7b\u7684\u65f6\u5019\uff0c<\/p>\n\n\n\n<p>\u5047\u5982<strong>\u4e0d\u9700\u8981\u7528\u5230\u4e0e\u7c7b\u76f8\u5173\u7684\u5c5e\u6027\u6216\u65b9\u6cd5<\/strong>\u65f6\uff0c\u5c31\u7528<strong>\u9759\u6001\u65b9\u6cd5@staticmethod<\/strong>\uff1b<\/p>\n\n\n\n<p>\u5047\u5982<strong>\u9700\u8981\u7528\u5230\u4e0e\u7c7b\u76f8\u5173\u7684\u5c5e\u6027\u6216\u65b9\u6cd5\uff0c\u7136\u540e\u53c8\u60f3\u8868\u660e\u8fd9\u4e2a\u65b9\u6cd5\u662f\u6574\u4e2a\u7c7b\u901a\u7528\u7684<\/strong>\uff0c\u800c<strong>\u4e0d\u662f\u5bf9\u8c61\u7279\u5f02\u7684<\/strong>\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528<strong>\u7c7b\u65b9\u6cd5@classmethod<\/strong>\u3002<\/p>\n\n\n\n<h2>\u88c5\u9970\u5668\u7684\u987a\u5e8f<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>@a\n@b\n@c\ndef f ():\n<\/code><\/pre>\n\n\n\n<p>\u7b49\u6548\u4e8e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>f = a(b(c(f)))<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>@\u7b26\u53f7\u662f\u88c5\u9970\u5668\uff08\u4fee\u9970\u7b26\uff09\u7684\u8bed\u6cd5\u7cd6\uff0c\u5728\u5b9a\u4e49\u51fd\u6570\u7684\u65f6\u5019\u4f7f\u7528\uff0c\u907f\u514d\u518d\u4e00\u6b21\u8d4b\u503c\u64cd\u4f5c \u88c5\u9970\u5668(Decorators)\u662f  &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/08\/23\/python-4\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">Python\u88c5\u9970\u5668\uff1apython\u4e2d\u7684@\u7b26\u53f7\u7684\u4f5c\u7528 \u4ee5\u53ca torch\u4e2d\u7ecf\u5e38\u51fa\u73b0\u7684 @torch.no_grad()<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8,11],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/6085"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=6085"}],"version-history":[{"count":39,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/6085\/revisions"}],"predecessor-version":[{"id":6133,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/6085\/revisions\/6133"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=6085"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=6085"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=6085"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}