Telegram Group & Telegram Channel
class Some:
def __hash__(self):
return 1

def __eq__(self, other):
return False


d = {Some() for _ in range(5)}

print(len(d))


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

Может показаться, что ответ = 1, т.к. hash любого инстанса класса Some равен 1.

1) При добавлении нового элемента в set, dict питон находит его hash(element) (если вы не знаете, что такое хэш функция и зачем она нужна, почитайте про хэш таблицы).

2) Из этого хэша определяется индекс в массиве, куда нужно вставить этот элемент.

3) Если по этому индексу ничего нет, то элемент просто вставляется.

4) А если там уже есть какой-то элемент (это коллизия), то питон уже сравнивает существуещий элемент с тем, который хотим добавить (new == old). Если они равны, то старый заменяется новым элементом.

5) В нашем случае все экземпляры Some разные, т.к.
__eq__ всегда возвращает False


Поэтому ответ 5.

#set #dict #hash



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

class Some:
def __hash__(self):
return 1

def __eq__(self, other):
return False


d = {Some() for _ in range(5)}

print(len(d))


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

Может показаться, что ответ = 1, т.к. hash любого инстанса класса Some равен 1.

1) При добавлении нового элемента в set, dict питон находит его hash(element) (если вы не знаете, что такое хэш функция и зачем она нужна, почитайте про хэш таблицы).

2) Из этого хэша определяется индекс в массиве, куда нужно вставить этот элемент.

3) Если по этому индексу ничего нет, то элемент просто вставляется.

4) А если там уже есть какой-то элемент (это коллизия), то питон уже сравнивает существуещий элемент с тем, который хотим добавить (new == old). Если они равны, то старый заменяется новым элементом.

5) В нашем случае все экземпляры Some разные, т.к.

__eq__ всегда возвращает False


Поэтому ответ 5.

#set #dict #hash

BY PyThrone


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

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

View MORE
Open in Telegram


telegram Telegram | DID YOU KNOW?

Date: |

Telegram today rolling out an update which brings with it several new features.The update also adds interactive emoji. When you send one of the select animated emoji in chat, you can now tap on it to initiate a full screen animation. The update also adds interactive emoji. When you send one of the select animated emoji in chat, you can now tap on it to initiate a full screen animation. This is then visible to you or anyone else who's also present in chat at the moment. The animations are also accompanied by vibrations. This is then visible to you or anyone else who's also present in chat at the moment. The animations are also accompanied by vibrations.

What is Telegram?

Telegram is a cloud-based instant messaging service that has been making rounds as a popular option for those who wish to keep their messages secure. Telegram boasts a collection of different features, but it’s best known for its ability to secure messages and media by encrypting them during transit; this prevents third-parties from snooping on messages easily. Let’s take a look at what Telegram can do and why you might want to use it.

telegram from vn


Telegram PyThrone
FROM USA