LoginSignup
0
0

More than 1 year has passed since last update.

WindowsによるPython入門 #03: 組み込み型とオブジェクト

Last updated at Posted at 2023-02-04

はじめに

今回は、組み込み型とオブジェクトに関して解説します。

YouTube動画

WindowsによるPython入門 (第三回) 組み込み型とオブジェクト

リテラルに関して

前回は変数を解説しましたが、変数に対して、直接記述する値の事をリテラルと言います。
例えば、整数の 1 や文字列の "abc" はリテラルです。
それぞれ、整数リテラルや文字列リテラルなどと言います。

組み込み型

予め使用可能な型を組み込み型と言います。
今のところ、整数型と文字列型を見ましたが、主要な組み込み型は以下の通りです。

  • bool型 (ブール型)
  • int型 (整数型)
  • float型 (浮動小数点数型)
  • bytearray (バイトアレイ型)
  • bytes型 (バイト列型)
  • str型 (文字列型)
  • tuple型 (タプル型)
  • list型 (リスト型)
  • dict型 (辞書型)
  • set型 (集合型)
  • frozenset型 (凍結集合型)

具体例を見てみます。

>>> type(True)  # ブール型
<class 'bool'>
>>> type(1)  # 整数型
<class 'int'>
>>> type(1.0)  # 浮動小数点数型
<class 'float'>
>>> type(bytearray(b"abc"))  # バイトアレイ型
<class 'bytearray'>
>>> type(b"abc")  # バイト列型
<class 'bytes'>
>>> type("abc")  # 文字列型
<class 'str'>
>>> type((1, 2, 3))  # タプル型
<class 'tuple'>
>>> type([1, 2, 3])  # リスト型
<class 'list'>
>>> type({"a":1, "b":2, "c":3})  # 辞書型
<class 'dict'>
>>> type({1, 2, 3})  # 集合型
<class 'set'>
>>> type(frozenset({1, 2, 3}))  # 凍結集合型
<class 'frozenset'>

bytearray 型や frozenset 型などリテラルが存在しないものもあります。
その場合、型から値を作ります。
各型の詳細に関しては、次回以降で解説します。

オブジェクトに関して

ここで、「型」に関して再度確認します。
整数リテラル 10 の型を確認すると、「クラス」int と表示されます。
Python では、クラスは型と同じ意味です。
つまり、int 型の値が整数値 10 でしたが、int クラスの値が 10 という事になります。

>>> type(10)
<class 'int'>

クラスというのは型で、型からインスタンス (実体) を作ります。
よくある説明ですが、型は「たい焼きの型」で、インスタンスが「たい焼き」です。
型の種類は色々ありますが、例えば、「たい焼きの型」や「今川焼の型」など型によって作成される実体は異なります。

instance.PNG

ここで、変数でも使用した図を再掲します。
インスタンスの中に値を持っています。
また、インスタンスはオブジェクトとも言います。
つまり正確に言えば、変数 a が指すのは「オブジェクト」で、オブジェクトであるクラスのインスタンスが整数値 10 を保持しています。
オブジェクトは「箱」のようなイメージです。

object_image..PNG

Pythonでは、基本的に変数や予約語以外、全てのものが「オブジェクト」です。
そして、変数はオブジェクトを参照しています。

クラスに関して

クラスの詳細は、後の回で解説しますが、ここでは簡単に説明します。
クラスのインスタンスは、属性として「変数」と「関数 (メソッド)」 を保持しています。
属性というのは、データの事です。
データで呼び出し可能なものがメソッドです。

具体的には、以下のような感じです。
文字列クラスのメソッドを呼び出しています。

>>> a = "abc"  # 変数 a は文字列オブジェクトを指す
>>> a.upper()  # 文字列の upper() メソッドの呼び出し
'ABC'
>>> a.startswith('a')  # 文字列の startswith() メソッドの呼び出し
True

オブジェクトの属性の一覧を確認するのは、dir() 関数を使用します。

>>> dir(a)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

おわりに

次回は、各型の詳細を解説していきます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0