Telegram Group & Telegram Channel
Что выведет этот код?

Iterable - это объект по которому можно пройтись. Для питона это любой объект у которого есть метод __iter__ или __getitem__.

Чтобы преобразовать какой то объект в list, нужно проитерироваться по объекту, т.е. вызвать iter(iterable).
Первым делом смотриться метод __iter__, если он возвращает iterator, то переберая все элементы итератора строиться list.
Но если у объекта нет метода __iter__, но есть метод __getitem__, то создается iterator на основе этого метода.
В метод __getitem__ по очередно передаются индексы (просто числа) от 0 до момента пока __getitem__ не выбросит ошибку StopIteration или IndexError.

Для чего это нужно?
На самом деле это очень старый механизм и в реальности почти не нужный. Так называемая "legacy feature".
Но может быть полезен, когда у вас есть какой то объект и вы хотите как то проитерироваться по нему.


class Library:
def __init__(self, books: list):
self.books = books

def __getitem__(self, index):
return self.books[index]

for book in Library(["a", "b", "c"]):
print(book)


#iter #getitem



tg-me.com/pythrone/14
Create:
Last Update:

Что выведет этот код?

Iterable - это объект по которому можно пройтись. Для питона это любой объект у которого есть метод __iter__ или __getitem__.

Чтобы преобразовать какой то объект в list, нужно проитерироваться по объекту, т.е. вызвать iter(iterable).
Первым делом смотриться метод __iter__, если он возвращает iterator, то переберая все элементы итератора строиться list.
Но если у объекта нет метода __iter__, но есть метод __getitem__, то создается iterator на основе этого метода.
В метод __getitem__ по очередно передаются индексы (просто числа) от 0 до момента пока __getitem__ не выбросит ошибку StopIteration или IndexError.

Для чего это нужно?
На самом деле это очень старый механизм и в реальности почти не нужный. Так называемая "legacy feature".
Но может быть полезен, когда у вас есть какой то объект и вы хотите как то проитерироваться по нему.


class Library:
def __init__(self, books: list):
self.books = books

def __getitem__(self, index):
return self.books[index]

for book in Library(["a", "b", "c"]):
print(book)


#iter #getitem

BY PyThrone




Share with your friend now:
tg-me.com/pythrone/14

View MORE
Open in Telegram


PyThrone Telegram | DID YOU KNOW?

Date: |

Importantly, that investor viewpoint is not new. It cycles in when conditions are right (and vice versa). It also brings the ineffective warnings of an overpriced market with it.Looking toward a good 2022 stock market, there is no apparent reason to expect these issues to change.

How To Find Channels On Telegram?

There are multiple ways you can search for Telegram channels. One of the methods is really logical and you should all know it by now. We’re talking about using Telegram’s native search option. Make sure to download Telegram from the official website or update it to the latest version, using this link. Once you’ve installed Telegram, you can simply open the app and use the search bar. Tap on the magnifier icon and search for a channel that might interest you (e.g. Marvel comics). Even though this is the easiest method for searching Telegram channels, it isn’t the best one. This method is limited because it shows you only a couple of results per search.

PyThrone from us


Telegram PyThrone
FROM USA