← Back to Python Course | Chapter 2: Input, Output & Operators | Lesson 14 of 16

Python Identity Operators

Identity operators पूछते हैं कि क्या दो नाम बिल्कुल एक ही object की तरफ इशारा करते हैं, न कि सिर्फ एक जैसी दिखने वाली चीज़ों की तरफ। यह पूछने जैसा है कि क्या दो बच्चे एक ही व्यक्ति हैं, न कि क्या वे जुड़वाँ हैं।
Syntax
python
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?

python
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

python
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

python
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

python
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

python
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
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.