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: |

In many cases, the content resembled that of the marketplaces found on the dark web, a group of hidden websites that are popular among hackers and accessed using specific anonymising software.“We have recently been witnessing a 100 per cent-plus rise in Telegram usage by cybercriminals,” said Tal Samra, cyber threat analyst at Cyberint.The rise in nefarious activity comes as users flocked to the encrypted chat app earlier this year after changes to the privacy policy of Facebook-owned rival WhatsApp prompted many to seek out alternatives.

What is Telegram Possible Future Strategies?

Cryptoassets enthusiasts use this application for their trade activities, and they may make donations for this cause.If somehow Telegram do run out of money to sustain themselves they will probably introduce some features that will not hinder the rudimentary principle of Telegram but provide users with enhanced and enriched experience. This could be similar to features where characters can be customized in a game which directly do not affect the in-game strategies but add to the experience.

telegram from ms


Telegram PyThrone
FROM USA