{"id":1150450,"date":"2025-01-13T17:01:42","date_gmt":"2025-01-13T09:01:42","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1150450.html"},"modified":"2025-01-13T17:01:45","modified_gmt":"2025-01-13T09:01:45","slug":"python%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%ad%e6%95%b0%e5%80%bc%e7%b1%bb%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1150450.html","title":{"rendered":"python\u5982\u4f55\u5224\u65ad\u6570\u503c\u7c7b\u578b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25180941\/c7464cb7-7e48-4ac2-bddc-6b2e9a8c2379.webp\" alt=\"python\u5982\u4f55\u5224\u65ad\u6570\u503c\u7c7b\u578b\" \/><\/p>\n<p><p> <strong>Python\u5224\u65ad\u6570\u503c\u7c7b\u578b\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684 <code>isinstance()<\/code> \u51fd\u6570\u3001\u68c0\u67e5\u6570\u503c\u7c7b\u578b\u7684\u5177\u4f53\u65b9\u6cd5\u3001\u4ee5\u53ca\u4f7f\u7528 <code>type()<\/code> \u51fd\u6570\u8fdb\u884c\u7c7b\u578b\u5224\u65ad\u7b49\u65b9\u5f0f\u3002<\/strong>\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 <code>isinstance()<\/code> \u51fd\u6570\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u5224\u65ad\u5355\u4e00\u7c7b\u578b\uff0c\u8fd8\u53ef\u4ee5\u5224\u65ad\u591a\u4e2a\u7c7b\u578b\u3002<strong>\u4f8b\u5982\uff0c<code>isinstance(x, (int, float))<\/code> \u53ef\u4ee5\u540c\u65f6\u68c0\u67e5 <code>x<\/code> \u662f\u5426\u4e3a\u6574\u6570\u6216\u6d6e\u70b9\u6570\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5177\u4f53\u6765\u8bf4\uff0c<code>isinstance()<\/code> \u51fd\u6570\u662f\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570\uff0c\u5b83\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\uff1a\u4e00\u4e2a\u5bf9\u8c61\u548c\u4e00\u4e2a\u7c7b\u578b\u6216\u7c7b\u578b\u7684\u5143\u7ec4\u3002\u5b83\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u8868\u793a\u5bf9\u8c61\u662f\u5426\u662f\u6307\u5b9a\u7c7b\u578b\u6216\u7c7b\u578b\u5143\u7ec4\u4e2d\u7684\u4e00\u4e2a\u3002\u4f7f\u7528 <code>isinstance()<\/code> \u51fd\u6570\u7684\u4f18\u70b9\u5728\u4e8e\u5b83\u53ef\u4ee5\u540c\u65f6\u68c0\u67e5\u591a\u4e2a\u7c7b\u578b\uff0c\u5e76\u4e14\u652f\u6301\u7ee7\u627f\u5173\u7cfb\u7684\u5224\u65ad\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 <code>isinstance()<\/code> \u51fd\u6570\u5224\u65ad\u6570\u503c\u7c7b\u578b<\/h3>\n<\/p>\n<p><p><code>isinstance()<\/code> \u51fd\u6570\u662f Python \u4e2d\u5224\u65ad\u5bf9\u8c61\u7c7b\u578b\u7684\u9996\u9009\u65b9\u5f0f\u3002\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u5224\u65ad\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u5c5e\u4e8e\u67d0\u4e2a\u7279\u5b9a\u7c7b\u578b\uff0c\u8fd8\u53ef\u4ee5\u5224\u65ad\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u5c5e\u4e8e\u591a\u4e2a\u7c7b\u578b\u4e2d\u7684\u4e00\u4e2a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_number(value):<\/p>\n<p>    return isinstance(value, (int, float, complex))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>is_number<\/code> \u51fd\u6570\u63a5\u6536\u4e00\u4e2a\u53c2\u6570 <code>value<\/code>\uff0c\u5e76\u4f7f\u7528 <code>isinstance()<\/code> \u51fd\u6570\u68c0\u67e5 <code>value<\/code> \u662f\u5426\u662f\u6574\u6570 (<code>int<\/code>)\u3001\u6d6e\u70b9\u6570 (<code>float<\/code>) \u6216\u590d\u6570 (<code>complex<\/code>) \u7c7b\u578b\u4e4b\u4e00\u3002\u5982\u679c <code>value<\/code> \u662f\u5176\u4e2d\u4e00\u79cd\u7c7b\u578b\uff0c<code>is_number<\/code> \u51fd\u6570\u8fd4\u56de <code>True<\/code>\uff0c\u5426\u5219\u8fd4\u56de <code>False<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u5728\u4e8e\u5b83\u7b80\u5355\u660e\u4e86\uff0c\u5e76\u4e14\u53ef\u4ee5\u540c\u65f6\u68c0\u67e5\u591a\u4e2a\u7c7b\u578b\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u60c5\u51b5\uff0c\u4f7f\u7528 <code>isinstance()<\/code> \u51fd\u6570\u6765\u5224\u65ad\u6570\u503c\u7c7b\u578b\u662f\u6700\u5408\u9002\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 <code>type()<\/code> \u51fd\u6570\u5224\u65ad\u6570\u503c\u7c7b\u578b<\/h3>\n<\/p>\n<p><p>\u867d\u7136 <code>isinstance()<\/code> \u51fd\u6570\u662f\u9996\u9009\uff0c\u4f46\u5728\u67d0\u4e9b\u7279\u5b9a\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528 <code>type()<\/code> \u51fd\u6570\u4e5f\u53ef\u4ee5\u8fbe\u5230\u7c7b\u4f3c\u7684\u6548\u679c\u3002<code>type()<\/code> \u51fd\u6570\u8fd4\u56de\u5bf9\u8c61\u7684\u7c7b\u578b\uff0c\u53ef\u4ee5\u4e0e\u67d0\u4e2a\u5177\u4f53\u7684\u7c7b\u578b\u8fdb\u884c\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_integer(value):<\/p>\n<p>    return type(value) == int<\/p>\n<p>def is_float(value):<\/p>\n<p>    return type(value) == float<\/p>\n<p>def is_complex(value):<\/p>\n<p>    return type(value) == complex<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e9b\u793a\u4f8b\u4e2d\uff0c<code>is_integer<\/code>\u3001<code>is_float<\/code> \u548c <code>is_complex<\/code> \u51fd\u6570\u5206\u522b\u68c0\u67e5 <code>value<\/code> \u662f\u5426\u662f\u6574\u6570\u3001\u6d6e\u70b9\u6570\u6216\u590d\u6570\u7c7b\u578b\u3002\u4e0e <code>isinstance()<\/code> \u51fd\u6570\u76f8\u6bd4\uff0c<code>type()<\/code> \u51fd\u6570\u7684\u7f3a\u70b9\u5728\u4e8e\u5b83\u53ea\u80fd\u68c0\u67e5\u5355\u4e00\u7c7b\u578b\uff0c\u4e0d\u80fd\u540c\u65f6\u68c0\u67e5\u591a\u4e2a\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528 <code>numbers<\/code> \u6a21\u5757\u5224\u65ad\u6570\u503c\u7c7b\u578b<\/h3>\n<\/p>\n<p><p>Python \u7684 <code>numbers<\/code> \u6a21\u5757\u5b9a\u4e49\u4e86\u62bd\u8c61\u57fa\u7c7b\uff0c\u5e2e\u52a9\u6211\u4eec\u8fdb\u884c\u6570\u503c\u7c7b\u578b\u7684\u5224\u65ad\u3002\u8fd9\u4e2a\u6a21\u5757\u63d0\u4f9b\u4e86 <code>Number<\/code>\u3001<code>Integral<\/code>\u3001<code>Real<\/code>\u3001<code>Rational<\/code> \u548c <code>Complex<\/code> \u7b49\u57fa\u7c7b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numbers<\/p>\n<p>def is_number(value):<\/p>\n<p>    return isinstance(value, numbers.Number)<\/p>\n<p>def is_integral(value):<\/p>\n<p>    return isinstance(value, numbers.Integral)<\/p>\n<p>def is_real(value):<\/p>\n<p>    return isinstance(value, numbers.Real)<\/p>\n<p>def is_rational(value):<\/p>\n<p>    return isinstance(value, numbers.Rational)<\/p>\n<p>def is_complex(value):<\/p>\n<p>    return isinstance(value, numbers.Complex)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e9b\u51fd\u6570\u5206\u522b\u4f7f\u7528 <code>isinstance()<\/code> \u51fd\u6570\u68c0\u67e5 <code>value<\/code> \u662f\u5426\u662f <code>numbers.Number<\/code>\u3001<code>numbers.Integral<\/code>\u3001<code>numbers.Real<\/code>\u3001<code>numbers.Rational<\/code> \u6216 <code>numbers.Complex<\/code> \u7c7b\u6216\u5176\u5b50\u7c7b\u7684\u5b9e\u4f8b\u3002<code>numbers<\/code> \u6a21\u5757\u7684\u4f18\u70b9\u5728\u4e8e\u5b83\u63d0\u4f9b\u4e86\u66f4\u62bd\u8c61\u548c\u7075\u6d3b\u7684\u6570\u503c\u7c7b\u578b\u68c0\u67e5\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u9700\u8981\u66f4\u7ec6\u7c92\u5ea6\u7c7b\u578b\u5224\u65ad\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u6570\u503c\u7c7b\u578b\u5224\u65ad<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u5224\u65ad\u8f93\u5165\u6570\u636e\u7684\u6570\u503c\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u5728\u6570\u636e\u6e05\u6d17\u548c\u9884\u5904\u7406\u9636\u6bb5\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u6570\u636e\u4e2d\u7684\u6570\u503c\u662f\u6b63\u786e\u7684\u7c7b\u578b\uff0c\u4ee5\u4fbf\u540e\u7eed\u7684\u8ba1\u7b97\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<p><h4>\u6570\u636e\u6e05\u6d17\u548c\u9884\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u6e05\u6d17\u548c\u9884\u5904\u7406\u9636\u6bb5\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u68c0\u67e5\u6570\u636e\u4e2d\u7684\u6570\u503c\u662f\u5426\u7b26\u5408\u9884\u671f\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u786e\u4fdd\u67d0\u5217\u6570\u636e\u4e2d\u7684\u6240\u6709\u503c\u90fd\u662f\u6574\u6570\u6216\u6d6e\u70b9\u6570\u3002\u5982\u679c\u53d1\u73b0\u6570\u636e\u4e2d\u5305\u542b\u975e\u6570\u503c\u7c7b\u578b\u7684\u6570\u636e\uff0c\u6211\u4eec\u9700\u8981\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\uff0c\u5982\u5220\u9664\u3001\u4e0d\u5305\u542b\u6216\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>def clean_data(data):<\/p>\n<p>    for column in data.columns:<\/p>\n<p>        if not all(isinstance(value, (int, float)) for value in data[column]):<\/p>\n<p>            data[column] = pd.to_numeric(data[column], errors=&#39;coerce&#39;)<\/p>\n<p>    return data.dropna()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>clean_data<\/code> \u51fd\u6570\u63a5\u6536\u4e00\u4e2a Pandas DataFrame \u5bf9\u8c61 <code>data<\/code>\uff0c\u5e76\u904d\u5386\u6bcf\u4e00\u5217\u6570\u636e\u3002\u5982\u679c\u67d0\u5217\u6570\u636e\u4e2d\u5305\u542b\u975e\u6574\u6570\u6216\u6d6e\u70b9\u6570\u7c7b\u578b\u7684\u6570\u636e\uff0c\u8be5\u5217\u5c06\u88ab\u8f6c\u6362\u4e3a\u6570\u503c\u7c7b\u578b\uff0c\u65e0\u6cd5\u8f6c\u6362\u7684\u503c\u5c06\u88ab\u66ff\u6362\u4e3a NaN\u3002\u6700\u540e\uff0c<code>clean_data<\/code> \u51fd\u6570\u8fd4\u56de\u5220\u9664\u6240\u6709\u5305\u542b NaN \u503c\u7684\u884c\u7684 DataFrame \u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h4>\u6570\u503c\u8ba1\u7b97\u548c\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u503c\u8ba1\u7b97\u548c\u5206\u6790\u9636\u6bb5\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u8f93\u5165\u6570\u636e\u7684\u6570\u503c\u7c7b\u578b\u6b63\u786e\uff0c\u4ee5\u4fbf\u8fdb\u884c\u6b63\u786e\u7684\u8ba1\u7b97\u548c\u5206\u6790\u3002\u4f8b\u5982\uff0c\u5728\u6267\u884c\u6570\u5b66\u8fd0\u7b97\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u6240\u6709\u8f93\u5165\u6570\u636e\u90fd\u662f\u6570\u503c\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def calculate_mean(values):<\/p>\n<p>    if all(isinstance(value, (int, float)) for value in values):<\/p>\n<p>        return sum(values) \/ len(values)<\/p>\n<p>    else:<\/p>\n<p>        r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(&quot;All values must be numbers&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>calculate_mean<\/code> \u51fd\u6570\u63a5\u6536\u4e00\u4e2a\u6570\u503c\u5217\u8868 <code>values<\/code>\uff0c\u5e76\u68c0\u67e5\u5217\u8868\u4e2d\u7684\u6240\u6709\u503c\u662f\u5426\u90fd\u662f\u6574\u6570\u6216\u6d6e\u70b9\u6570\u7c7b\u578b\u3002\u5982\u679c\u6240\u6709\u503c\u90fd\u662f\u6570\u503c\u7c7b\u578b\uff0c<code>calculate_mean<\/code> \u51fd\u6570\u8fd4\u56de\u5217\u8868\u7684\u5e73\u5747\u503c\uff1b\u5426\u5219\uff0c\u629b\u51fa\u4e00\u4e2a <code>ValueError<\/code> \u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728 Python \u4e2d\u5224\u65ad\u6570\u503c\u7c7b\u578b\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 <code>isinstance()<\/code> \u51fd\u6570\u3002<code>isinstance()<\/code> \u51fd\u6570\u4e0d\u4ec5\u53ef\u4ee5\u5224\u65ad\u5355\u4e00\u7c7b\u578b\uff0c\u8fd8\u53ef\u4ee5\u5224\u65ad\u591a\u4e2a\u7c7b\u578b\uff0c\u5e76\u4e14\u652f\u6301\u7ee7\u627f\u5173\u7cfb\u7684\u5224\u65ad\u3002\u5bf9\u4e8e\u66f4\u7ec6\u7c92\u5ea6\u7684\u6570\u503c\u7c7b\u578b\u5224\u65ad\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>numbers<\/code> \u6a21\u5757\u4e2d\u7684\u62bd\u8c61\u57fa\u7c7b\u3002\u6b64\u5916\uff0c<code>type()<\/code> \u51fd\u6570\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5224\u65ad\u6570\u503c\u7c7b\u578b\uff0c\u4f46\u53ea\u80fd\u68c0\u67e5\u5355\u4e00\u7c7b\u578b\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u5224\u65ad\u6570\u503c\u7c7b\u578b\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u6b63\u786e\u6027\u548c\u4e00\u81f4\u6027\uff0c\u4ece\u800c\u8fdb\u884c\u6b63\u786e\u7684\u8ba1\u7b97\u548c\u5206\u6790\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u68c0\u67e5\u4e00\u4e2a\u53d8\u91cf\u662f\u5426\u662f\u6570\u5b57\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>isinstance()<\/code>\u51fd\u6570\u6765\u5224\u65ad\u4e00\u4e2a\u53d8\u91cf\u662f\u5426\u4e3a\u6570\u5b57\u7c7b\u578b\u3002\u4f8b\u5982\uff0c<code>isinstance(variable, (int, float, complex))<\/code>\u53ef\u4ee5\u7528\u6765\u68c0\u67e5<code>variable<\/code>\u662f\u5426\u4e3a\u6574\u6570\u3001\u6d6e\u70b9\u6570\u6216\u590d\u6570\u3002\u8fd9\u6837\u7684\u505a\u6cd5\u53ef\u4ee5\u786e\u4fdd\u4f60\u5bf9\u4e0d\u540c\u7684\u6570\u5b57\u7c7b\u578b\u6709\u5168\u9762\u7684\u7406\u89e3\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u5224\u65ad\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u53ef\u4ee5\u8f6c\u6362\u4e3a\u6570\u5b57\uff1f<\/strong><br \/>\u8981\u68c0\u67e5\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u53ef\u4ee5\u8f6c\u6362\u4e3a\u6570\u5b57\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try<\/code>\u548c<code>except<\/code>\u8bed\u53e5\u7ed3\u5408<code>float()<\/code>\u51fd\u6570\u3002\u5982\u679c\u5b57\u7b26\u4e32\u53ef\u4ee5\u88ab\u6210\u529f\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\uff0c\u5219\u8bf4\u660e\u5b83\u662f\u4e00\u4e2a\u6709\u6548\u7684\u6570\u5b57\u5b57\u7b26\u4e32\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">def is_numeric_string(s):\n    try:\n        float(s)\n        return True\n    except ValueError:\n        return False\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u80fd\u591f\u6709\u6548\u907f\u514d\u56e0\u7c7b\u578b\u4e0d\u5339\u914d\u800c\u5bfc\u81f4\u7684\u9519\u8bef\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u81ea\u5b9a\u4e49\u5bf9\u8c61\u7684\u6570\u5b57\u5224\u65ad\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5bf9\u8c61\uff0c\u5e76\u5e0c\u671b\u80fd\u591f\u5224\u65ad\u5b83\u662f\u5426\u53ef\u4ee5\u88ab\u89c6\u4e3a\u6570\u5b57\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0<code>__int__()<\/code>\u6216<code>__float__()<\/code>\u65b9\u6cd5\u3002\u8fd9\u6837\uff0c\u5f53\u4f60\u4f7f\u7528<code>isinstance()<\/code>\u6216\u8005\u76f4\u63a5\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u65f6\uff0cPython\u4f1a\u8c03\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">class MyNumber:\n    def __init__(self, value):\n        self.value = value\n    \n    def __int__(self):\n        return int(self.value)\n    \nmy_num = MyNumber(10)\nprint(isinstance(my_num, (int, float)))  # \u8fd9\u5c06\u8fd4\u56deFalse\nprint(isinstance(int(my_num), (int, float)))  # \u8fd9\u5c06\u8fd4\u56deTrue\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u81ea\u5b9a\u4e49\u5bf9\u8c61\u53ef\u4ee5\u517c\u5bb9\u6570\u5b57\u7c7b\u578b\u7684\u5224\u65ad\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5224\u65ad\u6570\u503c\u7c7b\u578b\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684 isinstance() \u51fd\u6570\u3001\u68c0\u67e5\u6570\u503c\u7c7b\u578b\u7684\u5177\u4f53\u65b9\u6cd5\u3001\u4ee5\u53ca\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1150459,"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\/1150450"}],"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=1150450"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1150450\/revisions"}],"predecessor-version":[{"id":1150462,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1150450\/revisions\/1150462"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1150459"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1150450"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1150450"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1150450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}