{"id":3768,"date":"2025-05-30T00:06:48","date_gmt":"2025-05-29T16:06:48","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3768"},"modified":"2025-05-30T00:06:48","modified_gmt":"2025-05-29T16:06:48","slug":"%e5%8f%af%e7%86%94%e6%80%a7%ef%bc%88fused-types-%e6%88%96-type-fusion%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/05\/30\/%e5%8f%af%e7%86%94%e6%80%a7%ef%bc%88fused-types-%e6%88%96-type-fusion%ef%bc%89\/","title":{"rendered":"\u201c\u53ef\u7194\u6027\u201d\uff08fused types \u6216 type fusion\uff09"},"content":{"rendered":"\n<p>\u5728 Python \u7684\u6807\u51c6\u8bed\u4e49\u4e2d\uff0c<strong>\u201c\u53ef\u7194\u6027\u201d\uff08fused types \u6216 type fusion\uff09<\/strong> \u5e76\u4e0d\u662f\u4e00\u4e2a\u5185\u5efa\u7684\u6570\u636e\u7c7b\u578b\u6216\u5b98\u65b9\u672f\u8bed\u3002\u4f46\u8fd9\u4e2a\u6982\u5ff5\u5e38\u51fa\u73b0\u5728\u5982\u4e0b\u4e24\u4e2a\u8bed\u5883\u4e2d\uff1a<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u5728 Cython \u4e2d\u7684\u201c\u53ef\u7194\u7c7b\u578b\uff08Fused Types\uff09\u201d<\/h2>\n\n\n\n<p><strong>\u53ef\u7194\u7c7b\u578b\uff08Fused Types\uff09<\/strong> \u662f Cython \u4e2d\u7684\u4e00\u4e2a\u9ad8\u7ea7\u7279\u6027\uff0c\u5141\u8bb8\u4f60\u4e3a\u591a\u4e2a C \u6570\u636e\u7c7b\u578b\u7f16\u5199\u6cdb\u578b\u4ee3\u7801\uff0c\u8fbe\u5230\u7c7b\u4f3c\u4e8e C++ \u6a21\u677f\u6216 Python \u6cdb\u578b\u7684\u6548\u679c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b\uff1aCython \u4e2d\u5b9a\u4e49\u53ef\u7194\u7c7b\u578b<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>from cython cimport floating\n\ncdef fused numeric:\n    int\n    float\n    double\n\ncdef numeric add(numeric a, numeric b):\n    return a + b<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>numeric<\/code> \u5c31\u662f\u4e00\u4e2a <strong>\u53ef\u7194\u7c7b\u578b<\/strong>\uff0c\u8fd9\u4e2a\u51fd\u6570 <code>add<\/code> \u4f1a\u5728\u7f16\u8bd1\u65f6\u4e3a\u6bcf\u79cd\u7c7b\u578b\uff08int\u3001float\u3001double\uff09\u5206\u522b\u751f\u6210\u4e00\u4e2a\u51fd\u6570\u7248\u672c\u3002<\/p>\n\n\n\n<p>\ud83d\udcd8 \u5b98\u65b9\u6587\u6863\u53c2\u8003\uff1a<br>\ud83d\udd17 <a href=\"https:\/\/cython.readthedocs.io\/en\/latest\/src\/userguide\/fusedtypes.html\">https:\/\/cython.readthedocs.io\/en\/latest\/src\/userguide\/fusedtypes.html<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u5728 NumPy \u4e2d\u201c\u7c7b\u578b\u878d\u5408\uff08dtype coercion\uff09\u201d<\/h2>\n\n\n\n<p>NumPy \u4e2d\u6ca1\u6709\u201cfused types\u201d\u7684\u672f\u8bed\uff0c\u4f46\u5b83\u786e\u5b9e\u4f1a\u5728 <strong>\u591a\u79cd\u6570\u636e\u7c7b\u578b\u6df7\u5408\u65f6\u81ea\u52a8\u8fdb\u884c\u7c7b\u578b\u63d0\u5347\uff08type promotion\uff09\u6216\u5408\u5e76\uff08coercion\uff09<\/strong>\uff0c\u8fd9\u6709\u65f6\u4e5f\u88ab\u8bef\u79f0\u4e3a\u201c\u53ef\u7194\u6027\u201d\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np\n\na = np.array(&#91;1, 2, 3], dtype=np.int32)\nb = np.array(&#91;1.0, 2.0, 3.0], dtype=np.float64)\n\nc = a + b\nprint(c.dtype)  # float64\uff0c\u53d1\u751f\u4e86\u7c7b\u578b\u63d0\u5347<\/code><\/pre>\n\n\n\n<p>\u8fd9\u4e0d\u662f\u201c\u53ef\u7194\u6027\u201d\u7684\u6b63\u5f0f\u6982\u5ff5\uff0c\u4f46\u4f53\u73b0\u4e86\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u201c\u878d\u5408\u884c\u4e3a\u201d\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001Python \u672c\u8eab\u7684\u6cdb\u578b\u652f\u6301\uff08\u975e\u201c\u53ef\u7194\u6027\u201d\uff09<\/h2>\n\n\n\n<p>\u5728 Python 3.5+ \u4e2d\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>typing.Union<\/code> \u6216 <code>typing.TypeVar<\/code> \u8868\u793a\u591a\u4e2a\u7c7b\u578b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from typing import Union\n\ndef square(x: Union&#91;int, float]) -&gt; float:\n    return x * x<\/code><\/pre>\n\n\n\n<p>\u8fd9\u5728\u9759\u6001\u7c7b\u578b\u68c0\u67e5\u4e2d\u76f8\u5f53\u4e8e\u652f\u6301\u201c\u7c7b\u578b\u7194\u5408\u201d\u6982\u5ff5\uff0c\u4f46\u53ea\u662f\u7c7b\u578b\u6ce8\u89e3\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u603b\u7ed3<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u672f\u8bed<\/th><th>\u6240\u5c5e\u73af\u5883<\/th><th>\u542b\u4e49<\/th><\/tr><\/thead><tbody><tr><td>Fused Types<\/td><td>Cython<\/td><td>\u4e00\u79cd\u7f16\u8bd1\u65f6\u6cdb\u578b\u673a\u5236\uff0c\u5141\u8bb8\u51fd\u6570\u6216\u7c7b\u652f\u6301\u591a\u79cd C \u7c7b\u578b<\/td><\/tr><tr><td>Type Promotion<\/td><td>NumPy<\/td><td>\u591a\u79cd dtype \u64cd\u4f5c\u65f6\uff0c\u81ea\u52a8\u63d0\u5347\u5230\u517c\u5bb9\u7684\u7c7b\u578b<\/td><\/tr><tr><td>Union \u7c7b\u578b\u6ce8\u89e3<\/td><td>Python typing<\/td><td>\u9759\u6001\u7c7b\u578b\u63d0\u793a\u4e2d\u652f\u6301\u201c\u591a\u4e2a\u5019\u9009\u7c7b\u578b\u201d\u7684\u5f62\u5f0f<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u63a8\u8350\u9605\u8bfb\u94fe\u63a5<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\ud83d\udcd8 Cython \u5b98\u65b9\u6587\u6863 Fused Types<br>\ud83d\udd17 <a href=\"https:\/\/cython.readthedocs.io\/en\/latest\/src\/userguide\/fusedtypes.html\">https:\/\/cython.readthedocs.io\/en\/latest\/src\/userguide\/fusedtypes.html<\/a><\/li>\n\n\n\n<li>\ud83d\udcd8 Python <code>typing<\/code> \u6587\u6863<br>\ud83d\udd17 <a href=\"https:\/\/docs.python.org\/3\/library\/typing.html\">https:\/\/docs.python.org\/3\/library\/typing.html<\/a><\/li>\n\n\n\n<li>\ud83d\udcd8 NumPy \u7c7b\u578b\u7cfb\u7edf\uff08type coercion\uff09\u8bf4\u660e<br>\ud83d\udd17 <a href=\"https:\/\/numpy.org\/doc\/stable\/user\/basics.types.html\">https:\/\/numpy.org\/doc\/stable\/user\/basics.types.html<\/a><\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Python \u7684\u6807\u51c6\u8bed\u4e49\u4e2d\uff0c\u201c\u53ef\u7194\u6027\u201d\uff08fused types \u6216 type fusion\uff09 \u5e76\u4e0d\u662f\u4e00\u4e2a\u5185 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3769,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[54],"tags":[],"class_list":["post-3768","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3768","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3768"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3768\/revisions"}],"predecessor-version":[{"id":3770,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3768\/revisions\/3770"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3769"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3768"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3768"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3768"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}