Функция hash() в Python позволяет вычислять хеш-значения для различных объектов. Обычно для целых чисел хеш совпадает с их значением, но есть исключения, которые могут удивить даже опытных программистов.
Разбираем, почему hash(-1) и hash(-2) в CPython возвращают одинаковое значение. Рассмотрим особенности работы hash(), внутреннюю реализацию хэширования целых чисел и причину специальной обработки -1.
Вопрос:
Что выведет функция hash() для следующих значений: 1, 0, -1, -2?
Knock Knock is a Tamil-language horror thriller that follows a central premise common to the genre: an ordinary setting disrupted by an intruding supernatural or criminal force. The narrative centers on characters who experience escalating terror after unexplained events begin occurring at night — typically involving knocking sounds that trigger fear, suspense, and a gradual uncovering of past secrets or malevolent presences. The film uses standard horror devices like jump scares, eerie sound design, shadowy cinematography, and confined settings to create tension.
Knock Knock received mixed reviews typical for niche horror films: some critics and viewers appreciated its atmosphere and effective moments of suspense, while others found the plot predictable or the execution uneven. Box office performance for such regional genre films often depends on word-of-mouth and festival/showcase exposure rather than wide commercial release. Knock Knock In Tamilyogi
3.5/5
The surge in searches for proves that Tamil audiences have a massive appetite for edge-of-your-seat thrillers. Whether it’s a dubbed Hollywood classic or a new local indie project, the "knock at the door" remains one of the most effective ways to start a scary story. Knock Knock is a Tamil-language horror thriller that
: The film explores themes of temptation, domestic security, and the consequences of moral failures, often leaning into a campy or satirical tone. The Tamilyogi Connection Knock Knock received mixed reviews typical for niche
hash() может показаться незначительной, важно помнить о ней при работе с хэш-функциями и структурами данных, основанных на хэшировании. В большинстве случаев вы не столкнетесь с проблемами, но знание этой детали поможет вам избежать потенциальных ошибок и лучше понимать внутреннее устройство Python.Ключевые выводы:
Для небольших целых чисел в Python используется оптимизация (интернирование).
hash(x) == x для большинства целых чисел, но hash(-1) == -2 из-за внутренней реализации и для предотвращения коллизий.
Это поведение является специфичным для CPython и может отличаться в других реализациях Python (например, PyPy).
Используйте == для сравнения значений и is для сравнения идентичности объектов.
Надеюсь, теперь эта загадка с hash(-1) стала немного понятнее!
hash(-1) всегда возвращает -2, поэтому hash(-1) == hash(-2).__hash__() в пользовательских классах.