Функция hash() в Python позволяет вычислять хеш-значения для различных объектов. Обычно для целых чисел хеш совпадает с их значением, но есть исключения, которые могут удивить даже опытных программистов.
Разбираем, почему hash(-1) и hash(-2) в CPython возвращают одинаковое значение. Рассмотрим особенности работы hash(), внутреннюю реализацию хэширования целых чисел и причину специальной обработки -1.
Вопрос:
Что выведет функция hash() для следующих значений: 1, 0, -1, -2?
The mature woman in cinema today is the anti-ingénue. She is gritty. She is sexual on her own terms. She is often wrong. She is often glorious. She carries the weight of decades of bad decisions and hard-won wisdom in the slump of her shoulders.
The turning point came in the late 2000s and early 2010s, driven by three forces: the rise of streaming services (which prioritized niche demographics), the #OscarsSoWhite movement (which broadened conversations about inclusion to include age), and a handful of landmark performances that refused to be ignored. MegaPack - Syren De Mer - Multi-Penetration MILF
The "invisible woman" trope is fading into the past. As mature women continue to break box office records and dominate awards circuits, they are proving that life—and art—only gets more interesting with age. The future of cinema isn't just young; it is experienced, resilient, and unapologetically mature. The mature woman in cinema today is the anti-ingénue
Cinema is also seeing a slow but steady rebellion against ageist beauty standards. There is a growing movement toward "radical aging"—the decision to appear on screen without heavy filters or excessive cosmetic intervention. She is often wrong
For decades, Hollywood operated under a cruel arithmetic. A male actor’s value compounded with age, accruing gravitas and leading-man status well into his sixties and seventies. For women, however, the industry imposed an expiration date often tied to their thirtieth birthday. Once the ingenue became the mother, and the mother became the grandmother, the screen door typically hit them on the way out.
hash() может показаться незначительной, важно помнить о ней при работе с хэш-функциями и структурами данных, основанных на хэшировании. В большинстве случаев вы не столкнетесь с проблемами, но знание этой детали поможет вам избежать потенциальных ошибок и лучше понимать внутреннее устройство Python.Ключевые выводы:
Для небольших целых чисел в Python используется оптимизация (интернирование).
hash(x) == x для большинства целых чисел, но hash(-1) == -2 из-за внутренней реализации и для предотвращения коллизий.
Это поведение является специфичным для CPython и может отличаться в других реализациях Python (например, PyPy).
Используйте == для сравнения значений и is для сравнения идентичности объектов.
Надеюсь, теперь эта загадка с hash(-1) стала немного понятнее!
hash(-1) всегда возвращает -2, поэтому hash(-1) == hash(-2).__hash__() в пользовательских классах.