Embed presentation
Downloaded 50 times


![[AD] Python オンライン学習サービス PyQ機械学習コンテンツをリリースhttps://pyq.jp/NEW!32017/9/8](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fhow-does-python-get-the-length-with-the-len-function-170908022013%2f75%2fPython-len-3-2048.jpg&f=jpg&w=240)











![ list.__len__() は中に持っている要素数を返します。 要素数はlist自身が知っていますlistに len() Adapterlist:[2, None, ‘Yo’]len()__len__3 int?TypeError3returnYesNo__len__152017/9/8](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fhow-does-python-get-the-length-with-the-len-function-170908022013%2f75%2fPython-len-15-2048.jpg&f=jpg&w=240)
























![class MyIterator:def __init__(self, obj):self.obj = objself.c = 0def __next__(self):try:r = self.obj[self.c]self.c += 1return rexcept IndexError:raise StopIterationdef __iter__(self):return selfnext() return値iteratorの実装例__next____iter__and402017/9/8](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fhow-does-python-get-the-length-with-the-len-function-170908022013%2f75%2fPython-len-40-2048.jpg&f=jpg&w=240)

![ __iter__() メソッドを実装した独自クラスを定義します このコンテナをforに与えると、辞書のキーのアルファベット順に、そのキーの値が繰り返されます独自のデータ型に iter(), next() Adapterclass MyContainer:def __init__(self, mapping):self.keys = sorted(mapping) # ソートして保持self.mapping = mapping # 値返し用def __iter__(self): # for文で呼ばれるreturn MyIterator(self)def __getitem__(self, idx): # MyIteratorから呼ばれるreturn self.mapping[self.keys[idx]]>>> list(MyContainer({'foo': 1, 'bar': 2, 'poke': 3, 'ah': 4}))[4, 2, 1, 3] 422017/9/8](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fhow-does-python-get-the-length-with-the-len-function-170908022013%2f75%2fPython-len-42-2048.jpg&f=jpg&w=240)















Pythonには文字列やリストなど、長さをもつオブジェクトがあります。長さを手に入れるには、文字列なら、name.length ではなく len(name) のようにlen関数を使います。len関数はどうやってnameに入っているオブジェクトの長さを手に入れているのでしょうか。if文にはTrue/Falseとなる条件式を指定しますが、それだけでなく文字や数字、自分で作ったデータ型も渡せます。if文はどうやって与えられたオブジェクトがTrueなのかFalseなのかを手に入れているのでしょうか。 この発表では、Pythonのプログラムがどうやって必要な情報を手に入れているのか、また、自分で実装するときにどう実装すればlen()やif文やfor文に指定できるのかを説明します。


![[AD] Python オンライン学習サービス PyQ機械学習コンテンツをリリースhttps://pyq.jp/NEW!32017/9/8](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fhow-does-python-get-the-length-with-the-len-function-170908022013%2f75%2fPython-len-3-2048.jpg&f=jpg&w=240)











![ list.__len__() は中に持っている要素数を返します。 要素数はlist自身が知っていますlistに len() Adapterlist:[2, None, ‘Yo’]len()__len__3 int?TypeError3returnYesNo__len__152017/9/8](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fhow-does-python-get-the-length-with-the-len-function-170908022013%2f75%2fPython-len-15-2048.jpg&f=jpg&w=240)
























![class MyIterator:def __init__(self, obj):self.obj = objself.c = 0def __next__(self):try:r = self.obj[self.c]self.c += 1return rexcept IndexError:raise StopIterationdef __iter__(self):return selfnext() return値iteratorの実装例__next____iter__and402017/9/8](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fhow-does-python-get-the-length-with-the-len-function-170908022013%2f75%2fPython-len-40-2048.jpg&f=jpg&w=240)

![ __iter__() メソッドを実装した独自クラスを定義します このコンテナをforに与えると、辞書のキーのアルファベット順に、そのキーの値が繰り返されます独自のデータ型に iter(), next() Adapterclass MyContainer:def __init__(self, mapping):self.keys = sorted(mapping) # ソートして保持self.mapping = mapping # 値返し用def __iter__(self): # for文で呼ばれるreturn MyIterator(self)def __getitem__(self, idx): # MyIteratorから呼ばれるreturn self.mapping[self.keys[idx]]>>> list(MyContainer({'foo': 1, 'bar': 2, 'poke': 3, 'ah': 4}))[4, 2, 1, 3] 422017/9/8](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fhow-does-python-get-the-length-with-the-len-function-170908022013%2f75%2fPython-len-42-2048.jpg&f=jpg&w=240)













