PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Python Identity Operators: is and is not (Memory Address Check) (पहचान ऑपरेटर्स: 'is' बनाम '==' (मेमोरी आईडी बनाम मान तुलना)) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: Python identity operators (is and is not) test whether two references point to the exact same object in memory by comparing id(a) == id(b), whereas equality (==) compares object values via __eq__(). Because None, True, and False are process-wide singletons, PEP 8 strictly recommends 'is None' and 'is not None' over equality checks.
अध्याय सार: पायथन के पहचान ऑपरेटर्स (is और is not) id(a) == id(b) की तुलना करके जांचते हैं कि क्या दो रेफरेंस मेमोरी में एक ही ऑब्जेक्ट की ओर इशारा कर रहे हैं, जबकि == ऑब्जेक्ट के मान (__eq__) की तुलना करता है। चूंकि None, True और False सिंगलटन ऑब्जेक्ट हैं, इसलिए PEP 8 के अनुसार हमेशा 'is None' और 'is not None' का उपयोग करना चाहिए।
Python Identity Operators: is and is not (Memory Address Check): Detailed Notes & Architecture (पहचान ऑपरेटर्स: 'is' बनाम '==' (मेमोरी आईडी बनाम मान तुलना))
1. Identity (is / is not) vs Value Equality (== / !=) (पहचान (is / is not) बनाम मान समानता (== / !=))
[EN] The operator 'is' evaluates to True if and only if both operands refer to the identical object instance in memory, matching id(a) == id(b) (where id() returns the object's memory address in CPython). The 'is not' operator returns the negation (id(a) != id(b)) and is PEP 8 preferred over 'not (a is b)'. In contrast, == invokes the __eq__() method to check content equivalence: two separate lists [1, 2] and [1, 2] are equal (== is True) yet live at different addresses (is is False). Unlike ==, identity operators cannot be overloaded or overridden by custom classes.
[HI] ऑपरेटर 'is' तभी True देता है जब दोनों ऑपरेंड मेमोरी में एक ही ऑब्जेक्ट इंस्टेंस को संदर्भित करें, अर्थात id(a) == id(b) सत्य हो (CPython में id() मेमोरी एड्रेस बताता है)। 'is not' इसका विपरीत (id(a) != id(b)) जांचता है और PEP 8 में 'not (a is b)' से बेहतर माना जाता है। इसके विपरीत == ऑपरेटर __eq__() द्वारा मानों की तुलना करता है: दो अलग लिस्ट [1, 2] और [1, 2] का मान समान (== True) है, लेकिन मेमोरी पता अलग (is False) है। == के विपरीत, यूजर-डिफाइंड क्लासेस 'is' के व्यवहार को ओवरराइड नहीं कर सकतीं।
2. Singletons and the PEP 8 'is None' Convention (सिंगलटन ऑब्जेक्ट्स और PEP 8 का 'is None' नियम)
[EN] In Python, None (of NoneType), True, and False are singletons—exactly one instance of each exists throughout the entire interpreter lifetime. PEP 8 explicitly mandates writing 'if x is None:' or 'if x is not None:' instead of 'if x == None:'. Besides faster pointer comparison in CPython, 'is None' cannot be broken by custom classes that implement unusual __eq__() methods (such as returning True when compared with None, or returning a non-boolean array like NumPy). Similarly, checking 'is True' is generally discouraged in favor of direct truthiness 'if flag:', unless distinguishing True specifically from 1.
[HI] पायथन में None, True और False सिंगलटन (singleton) हैं—पूरे प्रोग्राम के दौरान इनका केवल एक ही इंस्टेंस मेमोरी में रहता है। PEP 8 स्पष्ट निर्देश देता है कि None की जाँच के लिए 'x == None' के बजाय हमेशा 'x is None' या 'x is not None' लिखना चाहिए। मेमोरी पॉइंटर तुलना तेज होने के साथ-साथ यह सुरक्षित भी है, क्योंकि कोई कस्टम क्लास __eq__() को बदलकर == को भ्रमित कर सकती है पर 'is' को नहीं। ध्यान दें कि 1 == True सत्य होता है जबकि 1 is True असत्य होता है।
3. CPython Small Integer Caching and String Interning (CPython स्मॉल इंटीजर कैशिंग और स्ट्रिंग इंटर्निंग)
[EN] For memory efficiency, CPython pre-allocates and caches all integer objects in the range [-5, 256] at startup, so two separately computed results equaling 100 share the same object (is returns True). Integers outside [-5, 256] (such as int('300') created at runtime) normally produce distinct objects where == is True and 'is' is False. CPython also automatically interns short identifier-like strings (containing ASCII letters, digits, or underscores) and supports explicit interning via sys.intern(). Because caching/interning is a CPython implementation detail (and literal 'x is 5' triggers SyntaxWarning in Python 3.8+), never use 'is' to compare numbers or strings—always use ==.
[HI] मेमोरी बचाने के लिए CPython स्टार्टअप पर ही -5 से 256 तक के सभी पूर्णांक ऑब्जेक्ट्स को कैश कर लेता है, इसलिए इस सीमा में अलग-अलग गणना से बने समान मानों के लिए 'is' True आता है। [-5, 256] से बाहर के पूर्णांक (जैसे रनटाइम पर बना int('300')) सामान्यतः अलग मेमोरी एड्रेस पर बनते हैं, जहाँ == True पर 'is' False होता है। CPython आइडेंटिफायर जैसी स्ट्रिंग्स की स्वतः इंटर्निंग (interning) करता है और sys.intern() द्वारा मैनुअल इंटर्निंग भी संभव है। चूंकि कैशिंग केवल CPython का आंतरिक नियम है (और पायथन 3.8+ में लिटरल के साथ 'is' लिखने पर SyntaxWarning आता है), इसलिए संख्याओं या स्ट्रिंग्स के लिए कभी 'is' का प्रयोग न करें।
4. Aliasing, Copying, and Precedence (एलियासिंग (Aliasing), कॉपी और प्राथमिकता)
[EN] Simple assignment b = a creates an alias pointing to the identical object (a is b is True); mutating b therefore mutates a when the object is mutable. Creating a shallow copy via b = a.copy() or b = a[:] creates a new container object so a == b is True while a is b is False. Both 'is' and 'is not' sit at the comparison precedence tier (same as ==, !=, <, >), higher than 'not', 'and', 'or'. Hence they can also be chained, e.g., a is b is not None evaluates as (a is b) and (b is not None).
[HI] साधारण असाइनमेंट b = a एक एलियास बनाता है जिसमें दोनों नाम एक ही ऑब्जेक्ट की ओर संकेत करते हैं (a is b == True); अतः म्यूटेबल ऑब्जेक्ट में एक को बदलने पर दूसरा भी बदल जाता है। a.copy() या a[:] द्वारा कॉपी बनाने पर नया ऑब्जेक्ट बनता है, जहाँ a == b True और a is b False होता है। 'is' और 'is not' की प्राथमिकता अन्य तुलनात्मक ऑपरेटर्स (==, <) के बराबर तथा 'not', 'and', 'or' से अधिक होती है। अतः ये चेन्ड तुलना में भी भाग ले सकते हैं, जैसे a is b is not None का अर्थ (a is b) and (b is not None) है।
Syntax, Formula & Technical Example (Python Identity Operators: is and is not (Memory Address Check))
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b, a is c) # True False
print(a == c, a is not c) # True True
val = None
print(val is None) # True
x, y = int('300'), int('300')
print(x == y, x is y) # True False
Python Identity Operators: is and is not (Memory Address Check) Quick Reference & Comparison Table
| Expression | Mechanism Checked | Key Exam Fact |
|---|---|---|
| a is b | id(a) == id(b) (same object) | True only for same memory reference |
| a is not b | id(a) != id(b) (distinct objects) | Preferred over 'not (a is b)' |
| a == b | a.__eq__(b) (value equality) | Can be True when 'a is b' is False |
| x is None | Singleton pointer comparison | Mandated by PEP 8 instead of == None |
Python Identity Operators: is and is not (Memory Address Check) Top 20 Bilingual MCQs with Answers
Q1. What does the 'is' operator check in Python?
(पायथन में 'is' ऑपरेटर क्या जांचता है?)
(A) Whether two variables refer to the exact same object in memory (id(a) == id(b))
(B) Whether a value belongs to a list
(C) Whether two objects have equal values
(D) Whether two objects share the same data type only
Correct Answer: (A)
[EN] Identity operators compare object identity via memory addresses (id()), not content equality.
[HI] पहचान ऑपरेटर ऑब्जेक्ट के मान के बजाय यह जांचते हैं कि दोनों का मेमोरी एड्रेस (id()) एक ही है या नहीं।
Q2. What is the output of: a = [1, 2]; b = [1, 2]; print(a == b, a is b)?
(a = [1, 2]; b = [1, 2]; print(a == b, a is b) का आउटपुट क्या होगा?)
(A) True False
(B) True True
(C) False True
(D) False False
Correct Answer: (A)
[EN] Each list literal allocates a separate list object in memory, so their values match (== True) while identities differ (is False).
[HI] दोनों लिस्ट लिटरल मेमोरी में अलग-अलग ऑब्जेक्ट बनाते हैं; अतः मान समान (True) हैं पर पहचान अलग (False) है।
Q3. What is the output of: x = (1,); y = x; print(x is y)?
(x = (1,); y = x; print(x is y) का आउटपुट क्या होगा?)
(A) TypeError
(B) False
(C) True
(D) None
Correct Answer: (C)
[EN] Assigning y = x creates an alias to the exact same tuple object, so id(x) == id(y).
[HI] y = x लिखने से दोनों नाम एक ही टपल ऑब्जेक्ट की ओर इशारा करते हैं, इसलिए x is y सत्य होगा।
Q4. Which integer range does CPython pre-cache (intern) by default at startup?
(CPython स्टार्टअप पर डिफ़ॉल्ट रूप से किस पूर्णांक रेंज को कैश (intern) करता है?)
(A) -128 to 127
(B) 0 to 255
(C) 0 to 127
(D) -5 to 256
Correct Answer: (D)
[EN] CPython caches small integers from -5 through 256 inclusive, so separate runtime computations in this range often share identity.
[HI] CPython -5 से 256 तक के छोटे पूर्णांकों को पहले से मेमोरी में कैश कर लेता है।
Q5. What is the output of: print(True == 1, True is 1)?
(print(True == 1, True is 1) (या वेरिएबल से तुलना) का परिणाम क्या होता है?)
(A) True False
(B) False False
(C) True True
(D) False True
Correct Answer: (A)
[EN] True equals 1 numerically (bool subclasses int), so == is True, but True is a bool singleton and 1 is an int object, so 'is' is False.
[HI] संख्यात्मक रूप से True == 1 सत्य है, लेकिन दोनों अलग-अलग ऑब्जेक्ट/क्लास इंस्टेंस हैं इसलिए True is 1 असत्य है।
Q6. According to PEP 8, what is the recommended way to test if variable x is None?
(PEP 8 के अनुसार किसी वेरिएबल x के None होने की जाँच करने का सही तरीका क्या है?)
(A) if x is None:
(B) if x == None:
(C) if not x:
(D) if type(x) == None:
Correct Answer: (A)
[EN] PEP 8 mandates 'is None' / 'is not None' because None is a singleton and == can be customized via __eq__.
[HI] PEP 8 के अनुसार 'is None' / 'is not None' का उपयोग करना चाहिए क्योंकि None एक सिंगलटन है और __eq__ से == प्रभावित हो सकता है।
Q7. Why is 'if not x:' NOT equivalent to 'if x is None:'?
('if not x:' और 'if x is None:' एक समान क्यों नहीं हैं?)
(A) 'not x' only works on boolean variables
(B) 'is None' checks truthiness of x
(C) 'if not x:' raises TypeError when x is None
(D) 'if not x:' also triggers for 0, '', [], and False, whereas 'is None' triggers strictly for None
Correct Answer: (D)
[EN] Empty containers, zero, and False are all falsy (making 'not x' True), even though none of them is None.
[HI] 0, '', [] और False सभी फॉल्सी हैं (अतः not x सत्य होगा), जबकि वे None नहीं हैं।
Q8. Which built-in function returns the unique identity integer (memory address in CPython) of an object?
(कौन सा बिल्ट-इन फंक्शन किसी ऑब्जेक्ट की यूनिक पहचान संख्या (CPython में मेमोरी एड्रेस) लौटाता है?)
(A) id()
(B) addr()
(C) hash()
(D) ref()
Correct Answer: (A)
[EN] a is b is strictly equivalent to id(a) == id(b).
[HI] a is b पूरी तरह से id(a) == id(b) के समतुल्य होता है।
Q9. What happens when you write b = a.copy() for a list a = [10, 20]?
(लिस्ट a = [10, 20] के लिए b = a.copy() लिखने पर क्या होता है?)
(A) a == b is False and a is b is False
(B) a == b is True and a is b is False
(C) a == b is True and a is b is True
(D) b becomes an alias of a
Correct Answer: (B)
[EN] copy() creates a new outer list object with identical elements, so content matches (==) while memory address differs (is False).
[HI] copy() समान तत्वों वाला एक नया लिस्ट ऑब्जेक्ट बनाता है, इसलिए मान समान पर मेमोरी आईडी भिन्न होती है।
Q10. What does Python 3.8+ emit if you write literal syntax like x is 100?
(पायथन 3.8+ में x is 100 जैसा लिटरल लिखने पर क्या चेतावनी आती है?)
(A) DeprecationWarning only on floats
(B) ZeroDivisionError
(C) SyntaxWarning: 'is' with a literal
(D) IndentationError
Correct Answer: (C)
[EN] Python 3.8 introduced a SyntaxWarning whenever 'is' or 'is not' is used directly with int/str/bytes/tuple literals (except singletons like None).
[HI] पायथन 3.8+ में None को छोड़कर int/str आदि लिटरल के साथ 'is' लिखने पर SyntaxWarning: 'is' with a literal आती है।
Q11. Can a user-defined Python class customize the behavior of the 'is' operator?
(क्या कोई यूजर-डिफाइंड पायथन क्लास 'is' ऑपरेटर के व्यवहार को बदल (customize) सकती है?)
(A) Yes, by overriding __eq__()
(B) No, 'is' always compares object identity and cannot be overridden
(C) Yes, by defining __is__()
(D) Yes, by overriding __id__()
Correct Answer: (B)
[EN] While __eq__() customizes ==, identity operators 'is' and 'is not' are built into the runtime and cannot be overloaded.
[HI] __eq__() द्वारा == को बदला जा सकता है, लेकिन 'is' और 'is not' को ओवरलोड नहीं किया जा सकता।
Q12. Which module provides intern() to manually force identical string objects to share memory?
(समान स्ट्रिंग ऑब्जेक्ट्स को जबरदस्ती एक ही मेमोरी एड्रेस साझा कराने के लिए intern() किस मॉड्यूल में है?)
(A) sys
(B) gc
(C) os
(D) functools
Correct Answer: (A)
[EN] sys.intern(string) enters a string into CPython's internal table of interned strings, enabling fast 'is' comparisons.
[HI] sys.intern() स्ट्रिंग को आंतरिक इंटर्न टेबल में जोड़ता है जिससे समान स्ट्रिंग्स एक ही मेमोरी साझा करती हैं।
Q13. What is the output of: x = int('500'); y = int('500'); print(x == y, x is not y)?
(x = int('500'); y = int('500'); print(x == y, x is not y) का आउटपुट क्या होगा?)
(A) False False
(B) False True
(C) True False
(D) True True
Correct Answer: (D)
[EN] 500 lies outside CPython's [-5, 256] cache, so two runtime int('500') calls create distinct objects: equal (== True) and not identical (is not True).
[HI] 500 [-5, 256] कैश के बाहर है, इसलिए दोनों अलग ऑब्जेक्ट बनते हैं: मान समान (True) और पहचान भिन्न (is not True)।
Q14. What is the output of: a = 'hi'; b = a[:]; print(a is b)?
(a = 'hi'; b = a[:]; print(a is b) का आउटपुट क्या होगा?)
(A) TypeError
(B) False
(C) None
(D) True
Correct Answer: (D)
[EN] Because strings are immutable, slicing an entire string with a[:] returns a reference to the same string object in CPython.
[HI] स्ट्रिंग्स इम्यूटेबल होती हैं, इसलिए पूरा स्लाइस a[:] नया ऑब्जेक्ट बनाने के बजाय उसी स्ट्रिंग ऑब्जेक्ट का रेफरेंस लौटाता है।
Q15. What does 'not a is b' evaluate as, and which syntax does PEP 8 prefer?
('not a is b' कैसे हल होता है और PEP 8 किसे प्राथमिकता देता है?)
(A) Evaluates as not (a is b); PEP 8 prefers 'a is not b'
(B) Raises SyntaxError
(C) Evaluates as (not a) is b; PEP 8 prefers 'not a is b'
(D) Evaluates as a != b
Correct Answer: (A)
[EN] 'is' has higher precedence than 'not', so 'not a is b' means 'not (a is b)', and PEP 8 strongly prefers 'a is not b'.
[HI] 'is' की प्राथमिकता 'not' से अधिक है, इसलिए इसका अर्थ not (a is b) होता है; PEP 8 'a is not b' लिखने की सलाह देता है।
Q16. Which of the following statements about 'is' and '==' is NOT true?
('is' और '==' के बारे में निम्नलिखित में से कौन सा कथन सत्य नहीं है?)
(A) Mutable list copies created with list(a) satisfy a == b but not a is b
(B) If a is b is True for built-in types (except NaN), a == b is True
(C) If a == b is True, then a is b must also be True
(D) None is a singleton, so two variables holding None always satisfy a is b
Correct Answer: (C)
[EN] Two distinct objects (like two separate [1, 2] lists or large ints) can have equal values (a == b is True) at different memory addresses (a is b is False).
[HI] दो अलग-अलग ऑब्जेक्ट्स का मान समान (a == b सत्य) हो सकता है, भले ही उनके मेमोरी एड्रेस अलग (a is b असत्य) हों।
Q17. Within Python Identity Operators: is and is not (Memory Address Check), which expression directly maps to: "id(a) == id(b) (same object)"?
(Python Identity Operators: is and is not (Memory Address Check) में कौन सा Expression सीधे "id(a) == id(b) (same object)" से संबंधित है?)
(A) a is not b
(B) x is None
(C) a == b
(D) a is b
Correct Answer: (D)
[EN] In Python Identity Operators: is and is not (Memory Address Check), 'a is b' handles 'id(a) == id(b) (same object)' (True only for same memory reference).
[HI] Python Identity Operators: is and is not (Memory Address Check) में 'a is b' का कार्य/विवरण 'id(a) == id(b) (same object)' (True only for same memory reference) है।
Q18. Which option accurately describes 'a is not b' in Python Identity Operators: is and is not (Memory Address Check)?
(Python Identity Operators: is and is not (Memory Address Check) के संदर्भ में 'a is not b' की सही विशेषता कौन सी है?)
(A) id(a) != id(b) (distinct objects)
(B) id(a) == id(b) (same object)
(C) Singleton pointer comparison
(D) a.__eq__(b) (value equality)
Correct Answer: (A)
[EN] 'a is not b' provides 'id(a) != id(b) (distinct objects)' — key point: Preferred over 'not (a is b)'.
[HI] 'a is not b' का मुख्य विवरण 'id(a) != id(b) (distinct objects)' है (Preferred over 'not (a is b)')।
Q19. In Python Identity Operators: is and is not (Memory Address Check), identify the correct parameter for the specification: "a.__eq__(b) (value equality)".
(Python Identity Operators: is and is not (Memory Address Check) में "a.__eq__(b) (value equality)" विवरण किस विकल्प के लिए सही है?)
(A) a is not b
(B) x is None
(C) a is b
(D) a == b
Correct Answer: (D)
[EN] 'a == b' corresponds to 'a.__eq__(b) (value equality)' (Can be True when 'a is b' is False).
[HI] 'a == b' का संबंध 'a.__eq__(b) (value equality)' (Can be True when 'a is b' is False) से है।
Q20. Considering Python Identity Operators: is and is not (Memory Address Check), what key behavior defines 'x is None'?
(Python Identity Operators: is and is not (Memory Address Check) में 'x is None' की मुख्य भूमिका या मान क्या है?)
(A) a.__eq__(b) (value equality)
(B) id(a) != id(b) (distinct objects)
(C) Singleton pointer comparison
(D) id(a) == id(b) (same object)
Correct Answer: (C)
[EN] 'x is None' represents 'Singleton pointer comparison' (Mandated by PEP 8 instead of == None).
[HI] 'x is None' का अर्थ/मान 'Singleton pointer comparison' है (Mandated by PEP 8 instead of == None)।
Python Identity Operators: is and is not (Memory Address Check) — Exam FAQs
Q1: Why did a = 256; b = 256 give True for 'a is b', while a = 257; b = 257 in separate inputs gave False?
a = 256; b = 256 पर 'a is b' True और अलग-अलग लाइन में a = 257; b = 257 पर False क्यों आता है?
[EN] CPython pre-caches integers from -5 to 256 at startup, so 256 always reuses the same object. Outside that range (257), interactive commands allocate separate int objects unless compiled in the same code block.
[HI] CPython -5 से 256 तक के पूर्णांकों को पहले से कैश करता है इसलिए 256 एक ही ऑब्जेक्ट रहता है, जबकि 257 अलग-अलग कथनों में नए ऑब्जेक्ट के रूप में बनता है।
Q2: Should I ever use 'is' instead of '==' for comparing strings or numbers?
क्या मुझे स्ट्रिंग्स या संख्याओं की तुलना के लिए == के बजाय 'is' का प्रयोग करना चाहिए?
[EN] No! Always use == for comparing values of numbers, strings, lists, etc. Reserve 'is' and 'is not' strictly for checking None (or intentional object-identity checks).
[HI] नहीं! संख्याओं, स्ट्रिंग्स या लिस्ट के मान मिलाने के लिए हमेशा == का उपयोग करें। 'is' और 'is not' का प्रयोग केवल None की जाँच या मेमोरी पहचान जांचने के लिए करें।
Q3: Can 'a is b' be True while 'a == b' is False?
क्या कभी 'a is b' सत्य और 'a == b' असत्य हो सकता है?
[EN] Yes, with float('nan'): if x = float('nan'), then 'x is x' is True (same object), whereas 'x == x' is False under IEEE 754 rules!
[HI] हाँ, float('nan') के मामले में: यदि x = float('nan') हो, तो 'x is x' (एक ही ऑब्जेक्ट) सत्य होगा, लेकिन IEEE 754 के कारण 'x == x' असत्य होगा।
Post a Comment
Post a Comment