tag:crieit.net,2005:https://crieit.net/tags/Python%E5%AD%A6%E7%BF%92%E6%97%A5%E8%A8%98/feed 「Python学習日記」の記事 - Crieit Crieitでタグ「Python学習日記」に投稿された最近の記事 2021-05-05T10:24:13+09:00 https://crieit.net/tags/Python%E5%AD%A6%E7%BF%92%E6%97%A5%E8%A8%98/feed tag:crieit.net,2005:PublicArticle/17058 2021-05-04T19:55:55+09:00 2021-05-05T10:24:13+09:00 https://crieit.net/posts/type type()関数で型のチェック <p>JSの<code>typeof</code>演算子や、Rubyの<code>.class</code>メソッド(※)のように、あるデータの型?クラス?を調べたいときは、Pythonでは <code>type()</code> 関数を使う。</p> <p>※ Rubyの<code>.class</code>メソッド:<br /> Rubyでは引数を囲う<code>()</code>を省略することができ、引数がない場合は省略するのが一般的なので、<code>.class</code><em>メソッド</em> と書いています。JSやPythonに慣れていると<em>プロパティ</em>の誤記かな?と思われるかもしれません。実際、括弧を省略したメソッドと純粋なプロパティとの間にどんな差異があるか自分自身わかっていないのですが、<a target="_blank" rel="nofollow noopener" href="https://docs.ruby-lang.org/ja/latest/class/Object.html#I_CLASS">公式にもメソッドとして記載があるので</a>、メソッドと表記しました。</p> <pre><code class="bash"># Python 3.6.5 Repl >>> type(23) <class 'int'> >>> type('hoge') <class 'str'> >>> type(True) <class 'bool'> </code></pre> <p><code>type()</code>関数の返り値のtypeは<code>type</code>になるみたい。</p> <pre><code class="bash">>>> type(type(23)) <class 'type'> </code></pre> mkataoka73