{"id":204460,"date":"2025-05-29T08:35:34","date_gmt":"2025-05-29T00:35:34","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204460\/"},"modified":"2025-05-29T08:35:34","modified_gmt":"2025-05-29T00:35:34","slug":"python-%e5%8d%95%e5%85%83%e6%b5%8b%e8%af%95%e4%b8%ad%e9%81%87%e5%88%b0-attributeerror-testemployee-object-has-no-attribute-employee%ef%bc%8c%e6%80%8e%e4%b9%88%e5%8a%9e%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204460\/","title":{"rendered":"Python \u5355\u5143\u6d4b\u8bd5\u4e2d\u9047\u5230 AttributeError: \\&#8217;TestEmployee\\&#8217; object has no attribute \\&#8217;employee\\&#8217;\uff0c\u600e\u4e48\u529e\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python \u5355\u5143\u6d4b\u8bd5\u4e2d\u9047\u5230 AttributeError: \\&#8217;TestEmployee\\&#8217; object has no attribute \\&#8217;employee\\&#8217;\uff0c\u600e\u4e48\u529e\uff1f<\/h1>\n<p>\u5bf9\u4e8e\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0c\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300aPython \u5355\u5143\u6d4b\u8bd5\u4e2d\u9047\u5230 AttributeError: \\&#8217;TestEmployee\\&#8217; object has no attribute \\&#8217;employee\\&#8217;\uff0c\u600e\u4e48\u529e\uff1f\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241108\/1731040419672d94a305262.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>python\u4e2d\u7684attributeerror\u95ee\u9898<\/strong><\/p>\n<p>\u5728\u4f7f\u7528python\u65f6\uff0c\u9047\u5230\u4e86attributeerror\u5f02\u5e38\uff0c\u63d0\u793a\u6d88\u606f\u662f&#8221;testemployee&#8217; object has no attribute &#8217;employee&#8221;\u3002\u8fd9\u662f\u600e\u4e48\u56de\u4e8b\uff1f<\/p>\n<p><strong>\u95ee\u9898\u5206\u6790<\/strong><\/p>\n<p>\u8fd9\u4e2a\u95ee\u9898\u51fa\u73b0\u5728\u5355\u5143\u6d4b\u8bd5\u4ee3\u7801\u4e2d\u3002\u5728\u4e3b\u4ee3\u7801\u4e2d\uff0c\u5b9e\u4f8b\u5316\u4e86\u4e00\u4e2a\u540d\u4e3a employee \u7684 employee \u5bf9\u8c61\uff0c\u5e76\u5c06\u5b83\u8d4b\u503c\u7ed9 self.employee \u3002\u4f46\u662f\uff0c\u5728\u6d4b\u8bd5\u65b9\u6cd5 test_give_default_raise \u4e2d\uff0c\u51fa\u73b0\u4e86\u9519\u8bef\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u9700\u8981\u68c0\u67e5 setup \u65b9\u6cd5\u662f\u5426\u5199\u9519\u4e86\u3002\u5728 python \u5355\u5143\u6d4b\u8bd5\u4e2d\uff0csetup \u65b9\u6cd5\u4f1a\u5728\u6bcf\u4e2a\u6d4b\u8bd5\u65b9\u6cd5\u8fd0\u884c\u4e4b\u524d\u88ab\u8c03\u7528\uff0c\u7528\u6765\u521d\u59cb\u5316\u6d4b\u8bd5\u73af\u5883\u3002\u8fd9\u91cc\uff0c\u628a setup \u5199\u6210\u4e86 setup\uff0c\u5bfc\u81f4 python \u65e0\u6cd5\u8bc6\u522b\u8be5\u65b9\u6cd5\u3002<\/p>\n<p>\u4fee\u6b63\u540e\u7684 setup \u65b9\u6cd5\u5199\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>def setUp(self):\n    self.employee = Employee('taylor', 'swift', 20000)<\/pre>\n<p>\u4fee\u6539\u540e\uff0csetup \u65b9\u6cd5\u5c06\u6b63\u786e\u5b9e\u4f8b\u5316 employee \u5bf9\u8c61\uff0c\u5e76\u5c06\u5176\u5206\u914d\u7ed9 self.employee \u3002\u8fd9\u5c06\u89e3\u51b3 attributeerror \u5f02\u5e38\uff0c\u5355\u5143\u6d4b\u8bd5\u5c06\u80fd\u591f\u6b63\u5e38\u8fd0\u884c\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300aPython \u5355\u5143\u6d4b\u8bd5\u4e2d\u9047\u5230 AttributeError: \\&#8217;TestEmployee\\&#8217; object has no attribute \\&#8217;employee\\&#8217;\uff0c\u600e\u4e48\u529e\uff1f\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u5355\u5143\u6d4b\u8bd5\u4e2d\u9047\u5230 A&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204460","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204460","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204460"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204460\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}