{"id":972897,"date":"2024-12-27T05:52:28","date_gmt":"2024-12-26T21:52:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/972897.html"},"modified":"2024-12-27T05:52:30","modified_gmt":"2024-12-26T21:52:30","slug":"python%e8%b5%8b%e5%80%bc%e5%a6%82%e4%bd%95%e5%8a%a0%e5%8d%95%e4%bd%8d","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/972897.html","title":{"rendered":"python\u8d4b\u503c\u5982\u4f55\u52a0\u5355\u4f4d"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24195333\/d2d57291-9fb2-461c-b855-187151fe57e0.webp\" alt=\"python\u8d4b\u503c\u5982\u4f55\u52a0\u5355\u4f4d\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u7ed9\u53d8\u91cf\u8d4b\u503c\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u7c7b\u3001\u5b57\u5178\u6216\u8005\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u7b49\u65b9\u5f0f\u4e3a\u53d8\u91cf\u6dfb\u52a0\u5355\u4f4d\u3002<\/strong> \u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u81ea\u5b9a\u4e49\u7c7b\u6765\u5c01\u88c5\u6570\u503c\u548c\u5355\u4f4d\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u7ba1\u7406\u548c\u64cd\u4f5c\u5e26\u5355\u4f4d\u7684\u6570\u636e\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7c7b\u6765\u8868\u793a\u7269\u7406\u91cf\uff0c\u5e76\u5728\u5176\u4e2d\u5305\u542b\u6570\u503c\u548c\u5355\u4f4d\u5c5e\u6027\u3002\u8fd9\u6837\u4e0d\u4ec5\u4fbf\u4e8e\u6570\u636e\u7684\u5b58\u50a8\u548c\u8f93\u51fa\uff0c\u4e5f\u6709\u52a9\u4e8e\u8fdb\u884c\u5355\u4f4d\u8f6c\u6362\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4e0d\u540c\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u81ea\u5b9a\u4e49\u7c7b\u5b9e\u73b0\u53d8\u91cf\u8d4b\u503c\u52a0\u5355\u4f4d<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u81ea\u5b9a\u4e49\u7c7b\u662f\u5b9e\u73b0\u4e3a\u53d8\u91cf\u8d4b\u503c\u52a0\u5355\u4f4d\u7684\u4e00\u4e2a\u5e38\u7528\u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u53ef\u4ee5\u5c06\u6570\u503c\u548c\u5355\u4f4d\u5c01\u88c5\u5728\u4e00\u4e2a\u5bf9\u8c61\u4e2d\uff0c\u65b9\u4fbf\u540e\u7eed\u7684\u64cd\u4f5c\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class PhysicalQuantity:<\/p>\n<p>    def __init__(self, value, unit):<\/p>\n<p>        self.value = value<\/p>\n<p>        self.unit = unit<\/p>\n<p>    def __str__(self):<\/p>\n<p>        return f&quot;{self.value} {self.unit}&quot;<\/p>\n<p>    def convert_to(self, new_unit):<\/p>\n<p>        # \u5355\u4f4d\u8f6c\u6362\u903b\u8f91\uff08\u9700\u6839\u636e\u5177\u4f53\u5355\u4f4d\u5173\u7cfb\u5b9e\u73b0\uff09<\/p>\n<p>        pass<\/p>\n<h2><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/h2>\n<p>length = PhysicalQuantity(5, &#39;meters&#39;)<\/p>\n<p>print(length)  # \u8f93\u51fa: 5 meters<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b9e\u73b0\u5355\u4f4d\u8f6c\u6362\u529f\u80fd<\/strong>\uff1a\u5728<code>PhysicalQuantity<\/code>\u7c7b\u4e2d\uff0c\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e2a\u65b9\u6cd5\u6765\u5b9e\u73b0\u5355\u4f4d\u4e4b\u95f4\u7684\u8f6c\u6362\u3002\u8fd9\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u5355\u4f4d\u5173\u7cfb\u8fdb\u884c\u5b9e\u73b0\uff0c\u6bd4\u5982\u7c73\u5230\u5398\u7c73\u3001\u5343\u514b\u5230\u514b\u7b49\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u589e\u5f3a\u7684\u6570\u503c\u548c\u5355\u4f4d\u64cd\u4f5c<\/strong>\uff1a\u901a\u8fc7\u91cd\u8f7d\u8fd0\u7b97\u7b26\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5bf9\u5e26\u5355\u4f4d\u7684\u6570\u503c\u7684\u52a0\u51cf\u4e58\u9664\u7b49\u8fd0\u7b97\u3002\u4f8b\u5982\uff0c\u5b9e\u73b0\u4e24\u4e2a<code>PhysicalQuantity<\/code>\u5bf9\u8c61\u76f8\u52a0\u65f6\uff0c\u9996\u5148\u9700\u8981\u68c0\u67e5\u5355\u4f4d\u662f\u5426\u76f8\u540c\uff0c\u7136\u540e\u518d\u8fdb\u884c\u6570\u503c\u76f8\u52a0\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5b57\u5178\u6216\u5143\u7ec4\u5b58\u50a8\u5355\u4f4d\u4fe1\u606f<\/p>\n<\/p>\n<p><p>\u5b57\u5178\u548c\u5143\u7ec4\u662fPython\u4e2d\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u7528\u6765\u7b80\u5355\u5730\u5b58\u50a8\u6570\u503c\u548c\u5355\u4f4d\u4fe1\u606f\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u6ca1\u6709\u81ea\u5b9a\u4e49\u7c7b\u7075\u6d3b\uff0c\u4f46\u5728\u7b80\u5355\u573a\u666f\u4e0b\u4e5f\u5f88\u5b9e\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">length = {&#39;value&#39;: 5, &#39;unit&#39;: &#39;meters&#39;}<\/p>\n<p>print(f&quot;{length[&#39;value&#39;]} {length[&#39;unit&#39;]}&quot;)  # \u8f93\u51fa: 5 meters<\/p>\n<h2><strong>\u6216\u8005\u4f7f\u7528\u5143\u7ec4<\/strong><\/h2>\n<p>length_tuple = (5, &#39;meters&#39;)<\/p>\n<p>print(f&quot;{length_tuple[0]} {length_tuple[1]}&quot;)  # \u8f93\u51fa: 5 meters<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7b80\u5355\u7684\u5c01\u88c5<\/strong>\uff1a\u4f7f\u7528\u5b57\u5178\u6216\u5143\u7ec4\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5c01\u88c5\u6570\u503c\u548c\u5355\u4f4d\uff0c\u4f46\u7f3a\u4e4f\u7c7b\u7684\u7075\u6d3b\u6027\uff0c\u65e0\u6cd5\u76f4\u63a5\u5b9e\u73b0\u590d\u6742\u7684\u5355\u4f4d\u8f6c\u6362\u548c\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9002\u7528\u4e8e\u7b80\u5355\u573a\u666f<\/strong>\uff1a\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u7528\u4e8e\u7b80\u5355\u7684\u6570\u636e\u5c55\u793a\u548c\u5b58\u50a8\uff0c\u4e0d\u9002\u5408\u9700\u8981\u590d\u6742\u64cd\u4f5c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u901a\u8fc7\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u5b9e\u73b0\u5355\u4f4d\u663e\u793a<\/p>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u662f\u4e00\u79cd\u7b80\u5355\u4e14\u76f4\u89c2\u7684\u65b9\u6cd5\uff0c\u7528\u4e8e\u5728\u8f93\u51fa\u65f6\u663e\u793a\u5355\u4f4d\u4fe1\u606f\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u6539\u53d8\u53d8\u91cf\u7684\u5b58\u50a8\u65b9\u5f0f\uff0c\u53ea\u662f\u5728\u5c55\u793a\u65f6\u9644\u52a0\u5355\u4f4d\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">value = 5<\/p>\n<p>unit = &#39;meters&#39;<\/p>\n<p>print(f&quot;{value} {unit}&quot;)  # \u8f93\u51fa: 5 meters<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7b80\u5355\u6613\u7528<\/strong>\uff1a\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u975e\u5e38\u7b80\u5355\uff0c\u9002\u5408\u5feb\u901f\u7684\u7ed3\u679c\u5c55\u793a\uff0c\u4e0d\u9700\u8981\u8fdb\u884c\u590d\u6742\u7684\u5355\u4f4d\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5c40\u9650\u6027<\/strong>\uff1a\u8fd9\u79cd\u65b9\u6cd5\u4ec5\u9650\u4e8e\u8f93\u51fa\u5c55\u793a\uff0c\u65e0\u6cd5\u8fdb\u884c\u5b9e\u9645\u7684\u5e26\u5355\u4f4d\u6570\u503c\u8fd0\u7b97\u548c\u903b\u8f91\u5904\u7406\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u4f7f\u7528Pint\u5e93\u8fdb\u884c\u5355\u4f4d\u7ba1\u7406<\/p>\n<\/p>\n<p><p>Pint\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u7269\u7406\u5355\u4f4d\u5904\u7406\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u5355\u4f4d\u7684\u5b9a\u4e49\u3001\u8f6c\u6362\u548c\u8fd0\u7b97\u3002\u4f7f\u7528Pint\u53ef\u4ee5\u907f\u514d\u624b\u52a8\u5b9e\u73b0\u590d\u6742\u7684\u5355\u4f4d\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pint import UnitRegistry<\/p>\n<p>ureg = UnitRegistry()<\/p>\n<p>length = 5 * ureg.meter<\/p>\n<p>print(length)  # \u8f93\u51fa: 5 meter<\/p>\n<p>print(length.to(&#39;centimeter&#39;))  # \u8f93\u51fa: 500.0 centimeter<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol>\n<li>\n<p><strong>\u81ea\u52a8\u5316\u5355\u4f4d\u5904\u7406<\/strong>\uff1aPint\u5e93\u53ef\u4ee5\u81ea\u52a8\u5904\u7406\u5404\u79cd\u5355\u4f4d\u8f6c\u6362\u548c\u8fd0\u7b97\uff0c\u51cf\u5c11\u4e86\u624b\u52a8\u7f16\u7801\u7684\u590d\u6742\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4e30\u5bcc\u7684\u5355\u4f4d\u652f\u6301<\/strong>\uff1aPint\u652f\u6301\u5927\u91cf\u7684\u5355\u4f4d\u548c\u7269\u7406\u91cf\uff0c\u9002\u5408\u9700\u8981\u5904\u7406\u591a\u79cd\u5355\u4f4d\u573a\u666f\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7075\u6d3b\u6027\u548c\u6269\u5c55\u6027<\/strong>\uff1aPint\u5141\u8bb8\u7528\u6237\u81ea\u5b9a\u4e49\u5355\u4f4d\u548c\u7269\u7406\u91cf\uff0c\u63d0\u4f9b\u4e86\u5f88\u9ad8\u7684\u7075\u6d3b\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0cPython\u53ef\u4ee5\u65b9\u4fbf\u5730\u4e3a\u53d8\u91cf\u8d4b\u503c\u52a0\u5355\u4f4d\u3002\u6839\u636e\u5b9e\u9645\u9700\u6c42\u7684\u590d\u6742\u7a0b\u5ea6\uff0c\u53ef\u4ee5\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u5355\u4f4d\u7ba1\u7406\u548c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u7ed9\u6570\u503c\u8d4b\u4e88\u5355\u4f4d\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u7c7b\u6765\u5b9e\u73b0\u6570\u503c\u7684\u5355\u4f4d\u8d4b\u503c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a<code>Measurement<\/code>\u7c7b\uff0c\u63a5\u53d7\u6570\u503c\u548c\u5355\u4f4d\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u63d0\u4f9b\u76f8\u5e94\u7684\u65b9\u6cd5\u6765\u8fdb\u884c\u5355\u4f4d\u8f6c\u6362\u548c\u8fd0\u7b97\u3002\u8fd9\u6837\uff0c\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u90fd\u4f1a\u63d0\u9ad8\u3002<\/p>\n<p><strong>Python\u662f\u5426\u6709\u5185\u7f6e\u7684\u5355\u4f4d\u8f6c\u6362\u529f\u80fd\uff1f<\/strong><br \/>Python\u6807\u51c6\u5e93\u5e76\u6ca1\u6709\u76f4\u63a5\u63d0\u4f9b\u5355\u4f4d\u8f6c\u6362\u7684\u529f\u80fd\uff0c\u4f46\u6709\u5f88\u591a\u7b2c\u4e09\u65b9\u5e93\uff0c\u4f8b\u5982<code>pint<\/code>\uff0c\u5b83\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u7269\u7406\u91cf\u53ca\u5176\u5355\u4f4d\u3002\u4f7f\u7528\u8fd9\u4e9b\u5e93\u53ef\u4ee5\u7b80\u5316\u5355\u4f4d\u5904\u7406\u7684\u8fc7\u7a0b\uff0c\u907f\u514d\u624b\u52a8\u8f6c\u6362\u53ef\u80fd\u5f15\u8d77\u7684\u9519\u8bef\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\u5355\u4f4d\u6362\u7b97\uff1f<\/strong><br \/>\u5728Python\u4e2d\u8fdb\u884c\u5355\u4f4d\u6362\u7b97\u65f6\uff0c\u901a\u5e38\u9700\u8981\u660e\u786e\u6e90\u5355\u4f4d\u548c\u76ee\u6807\u5355\u4f4d\u3002\u53ef\u4ee5\u5229\u7528\u7b2c\u4e09\u65b9\u5e93\u6216\u8005\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u6bd4\u5982\uff0c\u4f7f\u7528<code>pint<\/code>\u5e93\uff0c\u9996\u5148\u5b9a\u4e49\u91cf\u7684\u5355\u4f4d\uff0c\u7136\u540e\u901a\u8fc7\u7b80\u5355\u7684\u4e58\u6cd5\u6216\u9664\u6cd5\u8fdb\u884c\u6362\u7b97\uff0c\u786e\u4fdd\u5355\u4f4d\u7684\u4e00\u81f4\u6027\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u4e0d\u540c\u5355\u4f4d\u4e4b\u95f4\u7684\u8fd0\u7b97\uff1f<\/strong><br \/>\u5728\u5904\u7406\u4e0d\u540c\u5355\u4f4d\u4e4b\u95f4\u7684\u8fd0\u7b97\u65f6\uff0c\u63a8\u8350\u4f7f\u7528\u652f\u6301\u5355\u4f4d\u7684\u5e93\uff0c\u5982<code>pint<\/code>\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5728\u8fdb\u884c\u52a0\u6cd5\u3001\u51cf\u6cd5\u7b49\u8fd0\u7b97\u65f6\uff0c\u5355\u4f4d\u80fd\u591f\u81ea\u52a8\u8f6c\u6362\uff0c\u907f\u514d\u56e0\u5355\u4f4d\u4e0d\u4e00\u81f4\u800c\u5bfc\u81f4\u7684\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u7ed9\u53d8\u91cf\u8d4b\u503c\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u7c7b\u3001\u5b57\u5178\u6216\u8005\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u7b49\u65b9\u5f0f\u4e3a\u53d8\u91cf\u6dfb\u52a0\u5355\u4f4d\u3002 \u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":972910,"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\/972897"}],"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=972897"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/972897\/revisions"}],"predecessor-version":[{"id":972913,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/972897\/revisions\/972913"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/972910"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=972897"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=972897"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=972897"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}