Функция hash() в Python позволяет вычислять хеш-значения для различных объектов. Обычно для целых чисел хеш совпадает с их значением, но есть исключения, которые могут удивить даже опытных программистов.
Разбираем, почему hash(-1) и hash(-2) в CPython возвращают одинаковое значение. Рассмотрим особенности работы hash(), внутреннюю реализацию хэширования целых чисел и причину специальной обработки -1.
Вопрос:
Что выведет функция hash() для следующих значений: 1, 0, -1, -2?
You confront your demon, the Maw King. He offers a deal: give him your body for one hour to "fix" the world. He’s lying. But here’s the kicker—the game tracks every kill, every snack you stole, every time you ran from a fight.
Look for purple glowing particles in the environment. These scrolls unlock advanced combo routes in your skill tree. Essential Combat Tips
This comprehensive guide covers the core mechanics, main story progression, and essential character routes to help you navigate this life-simulation RPG. Getting Started: Core Gameplay Mechanics demon boy saga walkthrough
When cornered by the first mini-boss, do not fight head-on. Select [Evade and Counter] to expose his weak spot, saving your health potions for the chapter boss. Chapter 1 Boss: The Infernal Hound
Nicole is a central figure whose path depends heavily on your support during her financial struggles. You confront your demon, the Maw King
: Gameplay is divided into morning, afternoon, and night.
Recover the Broken Seal.
Demon Boy Saga is a masterclass in atmospheric horror and moral ambiguity. With this walkthrough, you now have the maps, puzzle solutions, boss tactics, and ending requirements to fully conquer the Netherveil. Whether you choose to save Ren’s soul or damn it forever, remember: every shadow hides a memory, and every choice has a consequence.
When she asks about your shifting eyes, reply: But here’s the kicker—the game tracks every kill,
: Dodge his overhead slams by dashing behind him. Strike his glowing ankles.
hash() может показаться незначительной, важно помнить о ней при работе с хэш-функциями и структурами данных, основанных на хэшировании. В большинстве случаев вы не столкнетесь с проблемами, но знание этой детали поможет вам избежать потенциальных ошибок и лучше понимать внутреннее устройство Python.Ключевые выводы:
Для небольших целых чисел в Python используется оптимизация (интернирование).
hash(x) == x для большинства целых чисел, но hash(-1) == -2 из-за внутренней реализации и для предотвращения коллизий.
Это поведение является специфичным для CPython и может отличаться в других реализациях Python (например, PyPy).
Используйте == для сравнения значений и is для сравнения идентичности объектов.
Надеюсь, теперь эта загадка с hash(-1) стала немного понятнее!
hash(-1) всегда возвращает -2, поэтому hash(-1) == hash(-2).__hash__() в пользовательских классах.