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


telegram Telegram | DID YOU KNOW?

Date: |

How Does Bitcoin Mining Work?

Bitcoin mining is the process of adding new transactions to the Bitcoin blockchain. It’s a tough job. People who choose to mine Bitcoin use a process called proof of work, deploying computers in a race to solve mathematical puzzles that verify transactions.To entice miners to keep racing to solve the puzzles and support the overall system, the Bitcoin code rewards miners with new Bitcoins. “This is how new coins are created” and new transactions are added to the blockchain, says Okoro.

Telegram Auto-Delete Messages in Any Chat

Some messages aren’t supposed to last forever. There are some Telegram groups and conversations where it’s best if messages are automatically deleted in a day or a week. Here’s how to auto-delete messages in any Telegram chat. You can enable the auto-delete feature on a per-chat basis. It works for both one-on-one conversations and group chats. Previously, you needed to use the Secret Chat feature to automatically delete messages after a set time. At the time of writing, you can choose to automatically delete messages after a day or a week. Telegram starts the timer once they are sent, not after they are read. This won’t affect the messages that were sent before enabling the feature.

telegram from kr


Telegram PyThrone
FROM USA