Python Identity Operators
In this page:
a is b
a is not b
Identity क्या है?
Python का हर object किसी विशिष्ट memory address पर रहता है, और identity का मतलब है यह जाँचना कि दो variable नाम ठीक उसी object की ओर इशारा कर रहे हैं, न कि केवल बराबर दिखने वाले values रखते हैं।
Built-in id() function उस address को दर्शाने वाली संख्या लौटाता है, और Python का is operator उन addresses की सीधी तुलना करता है।
उदाहरण: What Is Identity?
a = [1, 2]
b = [1, 2]
print(id(a) == id(b)) # different objects, so different memory addresses
print(a is b) # False, they are equal in value but not identical
is और is not
is operator तभी True लौटाता है जब दोनों operands शाब्दिक रूप से memory में एक ही object हों, जबकि is not उसका निषेध है।
यह == से अलग है, जो पूछता है कि क्या दो objects के values बराबर हैं भले ही वे अलग-अलग store हों — समान सामग्री वाले दो अलग list objects बराबर हैं पर identical नहीं।
उदाहरण: is and is not
a = [1, 2]
b = a # b now refers to the same list object as a
c = [1, 2] # c is a separate object with equal contents
print(a is b) # True, same object
print(a is not c) # True, different objects
व्यवहार में is बनाम ==
जब आपको value की बराबरी से मतलब हो, जैसे दो strings या संख्याओं की सामग्री की तुलना, तब == इस्तेमाल कीजिए, और is को identity की जाँच के लिए रखिए जैसे x is None, जो खास तौर पर None की जाँच का मुहावरेदार और अनुशंसित तरीका है।
None की तुलना के लिए == का उपयोग उन custom classes के साथ गलत व्यवहार कर सकता है जो बराबरी को override करती हैं।
उदाहरण: is vs == in Practice
x = None
print(x is None) # idiomatic identity check for None
print(x == None) # works here too, but is not the recommended style
छोटे Integer Cache की पेचीदगी
CPython प्रदर्शन के लिए छोटे integers (आमतौर पर -5 से 256) और छोटी strings को singleton objects के रूप में cache करती है, इसलिए a = 5; b = 5; a is b अक्सर संयोग से True लौटाता है, डिज़ाइन से नहीं।
सामान्य value की तुलना के लिए is पर निर्भर रहना bugs का आम स्रोत है, क्योंकि यह caching व्यवहार भाषा की specification में गारंटीशुदा नहीं है और बदल सकता है।
उदाहरण: The Small Integer Cache Gotcha
a = 5
b = 5
print(a is b) # True due to CPython's small-int caching, not guaranteed behavior
व्यावहारिक Identity जाँचें
Identity जाँचें sentinel values, is None या is True जैसी singleton तुलनाओं, और यह जाँचने में सबसे ज़्यादा उपयोगी हैं कि दो variables ठीक उसी mutable object का संदर्भ देते हैं या नहीं, ताकि तय हो सके कि एक को बदलना दूसरे को प्रभावित करेगा या नहीं।
यह खास तौर पर तब मायने रखता है जब functions के बीच lists जैसे mutable objects पास किए जाएँ।
उदाहरण: Practical Identity Checks
def process(items=None):
if items is None:
items = [] # a new list object is created each call
return items
a = process()
b = process()
print(a is b) # False, each call got its own list object
Chapter Quiz — Complete all 16 topics to unlock
0/16 topics done
Complete these topics first:
- Python print()
- Python input()
- Python Format Strings
- Python f-strings
- Python String Formatting
- Python Arithmetic Operators
- Python Relational Operators
- Python Logical Operators
- Python Bitwise Operators
- Python Assignment Operators
- Python Increment & Decrement
- Python Ternary Operator
- Python Operator Precedence
- Python Identity Operators
- Python Membership Operators
- Python Operators