Функция hash() в Python позволяет вычислять хеш-значения для различных объектов. Обычно для целых чисел хеш совпадает с их значением, но есть исключения, которые могут удивить даже опытных программистов.
Разбираем, почему hash(-1) и hash(-2) в CPython возвращают одинаковое значение. Рассмотрим особенности работы hash(), внутреннюю реализацию хэширования целых чисел и причину специальной обработки -1.
Вопрос:
Что выведет функция hash() для следующих значений: 1, 0, -1, -2?
Every time you write a sentence in the workbook, say it out loud. This bridges the gap between writing and speaking.
The I Love Korean 2 series is designed for learners who have a foundation but need to build stamina in speaking, listening, and writing. While the textbook introduces concepts, the is where the real magic happens. 1. Practical, Real-World Scenarios
Master Korean with Ease: Why the "I Love Korean 2" Workbook is a Game Changer i love korean 2 workbook pdf top
In the digital age, having your study materials on an iPad or laptop is a massive advantage. Here is why the is a top search:
The SNU curriculum is circular, meaning concepts repeat. If you struggle with a unit, go back to the corresponding section in Workbook 1 before moving forward. Every time you write a sentence in the
If you’ve moved past the basics of Hangul and introductory greetings, you know that the "intermediate plateau" is real. Finding the right resources to bridge the gap between "hello" and actual conversation is tough. That is exactly why searches for the have skyrocketed among dedicated learners.
Ready to level up your Korean? Grab your copy, fire up your favorite study playlist, and start practicing today! While the textbook introduces concepts, the is where
Unlike older academic texts that focus on stiff, formal grammar, this workbook focuses on active communication. You’ll find exercises centered around: Ordering specific dishes at a restaurant. Making plans with friends using natural transitions. Describing daily routines and past experiences with nuance. 2. Systematic Grammar Reinforcement
One of the reasons students look for the top digital versions of this workbook is the integration of audio. The workbook tasks often require you to listen to natural-speed Korean and respond, ensuring your ears are as sharp as your reading skills. Why Learners Prefer the PDF Version
Quickly find specific grammar points or vocabulary words using the "find" function.
hash() может показаться незначительной, важно помнить о ней при работе с хэш-функциями и структурами данных, основанных на хэшировании. В большинстве случаев вы не столкнетесь с проблемами, но знание этой детали поможет вам избежать потенциальных ошибок и лучше понимать внутреннее устройство Python.Ключевые выводы:
Для небольших целых чисел в Python используется оптимизация (интернирование).
hash(x) == x для большинства целых чисел, но hash(-1) == -2 из-за внутренней реализации и для предотвращения коллизий.
Это поведение является специфичным для CPython и может отличаться в других реализациях Python (например, PyPy).
Используйте == для сравнения значений и is для сравнения идентичности объектов.
Надеюсь, теперь эта загадка с hash(-1) стала немного понятнее!
hash(-1) всегда возвращает -2, поэтому hash(-1) == hash(-2).__hash__() в пользовательских классах.