Функция hash() в Python позволяет вычислять хеш-значения для различных объектов. Обычно для целых чисел хеш совпадает с их значением, но есть исключения, которые могут удивить даже опытных программистов.
Разбираем, почему hash(-1) и hash(-2) в CPython возвращают одинаковое значение. Рассмотрим особенности работы hash(), внутреннюю реализацию хэширования целых чисел и причину специальной обработки -1.
Вопрос:
Что выведет функция hash() для следующих значений: 1, 0, -1, -2?
"The Black Phone" takes place in the 1970s and revolves around the story of a young boy named Finney Blake (Mason Gooding), who finds himself in the clutches of a serial killer known as The Grabber (Emory Cohen). The Grabber is notorious for abducting young boys and subjecting them to unimaginable horrors. Finney, determined to escape and bring The Grabber to justice, uses his wit and resourcefulness to outsmart his captor.
As Finney navigates his terrifying ordeal, he discovers a mysterious black phone that allows him to communicate with The Grabber's previous victims. Through these conversations, Finney gains valuable insights and learns the dark secrets surrounding The Grabber's past. With the help of the black phone, Finney devises a plan to confront The Grabber and put an end to his sinister activities. Download - The.Black.Phone.2022.1080p.WeBDL.Hi...
In recent years, the film industry has witnessed a surge in the production of gripping and intense thriller movies, and "The Black Phone" is one such cinematic masterpiece that has been making waves among movie enthusiasts. Directed by Scott Derrickson and starring Mason Gooding, Madeleine McGraw, and Emory Cohen, this 2022 American supernatural thriller film has garnered significant attention for its captivating storyline and impressive performances. If you're interested in downloading "The Black Phone" in high-quality, you've come to the right place. "The Black Phone" takes place in the 1970s
hash() может показаться незначительной, важно помнить о ней при работе с хэш-функциями и структурами данных, основанных на хэшировании. В большинстве случаев вы не столкнетесь с проблемами, но знание этой детали поможет вам избежать потенциальных ошибок и лучше понимать внутреннее устройство Python.Ключевые выводы:
Для небольших целых чисел в Python используется оптимизация (интернирование).
hash(x) == x для большинства целых чисел, но hash(-1) == -2 из-за внутренней реализации и для предотвращения коллизий.
Это поведение является специфичным для CPython и может отличаться в других реализациях Python (например, PyPy).
Используйте == для сравнения значений и is для сравнения идентичности объектов.
Надеюсь, теперь эта загадка с hash(-1) стала немного понятнее!
hash(-1) всегда возвращает -2, поэтому hash(-1) == hash(-2).__hash__() в пользовательских классах.