isinstance(own,Hashable) قد تعطي True رغم فشل hash(own)؛ التعرف لا يثبت نجاح التنفيذ. [1] [2]
الخلاصة السريعة
لا؛ الطريقة قد ترفع خطأ. [1] [2]
- isinstance(own,Hashable) قد تعطي True رغم فشل hash(own)؛ التعرف لا يثبت نجاح التنفيذ. [1] [2]
- على CPython 3.12.14، own=Bad()؛ __hash__ تسجل "hash" ثم ترفع TypeError("bud"). [1] [2]
- فحص Hashable يعطي True وcalls تبقى []؛ لم ينفذ الطريقة في المثال. [1] [2]
- hash(own) ترفع TypeError؛ calls تصبح ["hash"]. [1] [2]
- تحكم: NoHash تعرف __hash__=None؛ التعرف False والهاش يرفع TypeError. Fixed تعيد 17 من طريقتها؛ التعرف True والهاش 17. الثابت تحكم للاختبار. [1] [2]
التعرف والتنفيذ
isinstance(own,Hashable) قد تعطي True رغم فشل hash(own)؛ التعرف لا يثبت نجاح التنفيذ. [1] [2]
طريقة ترفع خطأ
على CPython 3.12.14، own=Bad()؛ __hash__ تسجل "hash" ثم ترفع TypeError("bud"). [1] [2]
التعرف قبل النداء
فحص Hashable يعطي True وcalls تبقى []؛ لم ينفذ الطريقة في المثال. [1] [2]
from collections.abc import Hashable calls=[] class Bad: def __hash__(self):calls.append("hash");raise TypeError("bud") own=Bad() print(isinstance(own,Hashable),calls) try:hash(own) except TypeError:print("TypeError",calls) [1] [2]

النداء والتحكم
hash(own) ترفع TypeError؛ calls تصبح ["hash"]. [1] [2]
class NoHash:__hash__=None disabled=NoHash() print(isinstance(disabled,Hashable)) try:hash(disabled) except TypeError:print("TypeError") class Fixed: def __hash__(self):return 17 fixed=Fixed() print(isinstance(fixed,Hashable),hash(fixed)) [1] [2]
تحكم: NoHash تعرف __hash__=None؛ التعرف False والهاش يرفع TypeError. Fixed تعيد 17 من طريقتها؛ التعرف True والهاش 17. الثابت تحكم للاختبار. [1] [2]
المراجع
للتوسع: لغات البرمجة.
أُعدّ هذا المقال بمساعدة الذكاء الاصطناعي، وخضع لمراجعة ذاتية بالأداة نفسها. لا يُنسب إلى مراجع بشري أو خبير مستقل. منهجية المحتوى.
