: Furthering the personal storylines for early-game characters like Island Mystery
Arguments returned, but they now included questions of consent. Whose memories were these? Had the land given them up willingly? The chorus—if it could be called that—answered in small ways: a gravel path would bloom when stepped on by two people in step; a lamp would glare red when someone tried to pry a gear loose; a child could call a rain-cloud to wash away a tag that declared ownership.
Вопрос:
Что выведет функция hash() для следующих значений: 1, 0, -1, -2?
: Furthering the personal storylines for early-game characters like Island Mystery
Arguments returned, but they now included questions of consent. Whose memories were these? Had the land given them up willingly? The chorus—if it could be called that—answered in small ways: a gravel path would bloom when stepped on by two people in step; a lamp would glare red when someone tried to pry a gear loose; a child could call a rain-cloud to wash away a tag that declared ownership.
hash() может показаться незначительной, важно помнить о ней при работе с хэш-функциями и структурами данных, основанных на хэшировании. В большинстве случаев вы не столкнетесь с проблемами, но знание этой детали поможет вам избежать потенциальных ошибок и лучше понимать внутреннее устройство Python.Ключевые выводы:
Для небольших целых чисел в Python используется оптимизация (интернирование).
New World Paradise -v0.1.3.1- By DingoDeer
hash(x) == x для большинства целых чисел, но hash(-1) == -2 из-за внутренней реализации и для предотвращения коллизий.
New World Paradise -v0.1.3.1- By DingoDeer
Это поведение является специфичным для CPython и может отличаться в других реализациях Python (например, PyPy).
New World Paradise -v0.1.3.1- By DingoDeer
Используйте == для сравнения значений и is для сравнения идентичности объектов.
Надеюсь, теперь эта загадка с hash(-1) стала немного понятнее!
hash(-1) всегда возвращает -2, поэтому hash(-1) == hash(-2).__hash__() в пользовательских классах.