Shared Reference and In-Place Change

Shared Reference and In-Place Change – Python के List, Dictionary, Sets सहित कुछ ऐसे Mutable Types भी हैं, जिनमें किन्‍हीं Specific Operations की वजह से अथवा कुछ Specific Objects की वजह से In-Place Change भी Perform हो जाता है।

उदाहरण के लिए जब हम किसी List Object के Offset पर किसी नए Data Item को Assign करते हैं, तब हम वास्‍तव में उस Actual List में ही Change कर रहे होते हैं। इस स्थिति में कोई नया List Object Create नहीं होता बल्कि Current List Object में ही Actual Change हो रहा होता है।

इसलिए जब हम किसी Python Program में इस तरह के Mutable Objects के साथ Deal कर रहे होते हैं, तब इस तरह के Objects के Shared References को काफी सावधानीपूर्वक तरीके से Handle करना जरूरी होता है क्‍योंकि किसी एक Variable में किया गया Change अन्‍य सभी Shared Referenced Variables के Result को भी प्रभावित कर सकता है। इस Shared Reference व In-Place Change को एक Example द्वारा ज्‍यादा बेहतर तरीके से समझ सकते हैं-

[code]
FileName: SharedReferences.py
val1 = 12
val2 = val1
print("Value of val1:", val1)
print("Value of val2:", val2)
print("Change Value of val1 to 'spam'")
val1 = 'spam'
print()
print("After changing Value of val1 to 'spam'")
print("Value of val1:", val1)
print("Value of val2 is still:", val2)
print()
print("Apply Same Shared Reference Process on Mutable List")
lst1 = [0, 1, 2, 3, 4]
lst2 = lst1
print("Data Items of lst1:", lst1)
print("Data Items of lst2:", lst2)
print("Change Value of lst1's Index Number 2 to 'spam'")
lst2[2] = 'spam'
print()
print("After Adding 'spam' at lst1's Index Number 2")
print("Data Items of lst1:", lst1)
print("Data Items of lst2:", lst2)

Output
Value of val1: 12
Value of val2: 12

Change Value of val1 to 'spam'

After changing Value of val1 to 'spam'
Value of val1: spam
Value of val2 is still: 12

Apply Same Shared Reference Process on Mutable List
Data Items of lst1: [0, 1, 2, 3, 4]
Data Items of lst2: [0, 1, 2, 3, 4]

Change Value of lst1's Index Number 2 to 'spam'

After Adding 'spam' at lst1's Index Number 2
Data Items of lst1: [0, 1, 'spam', 3, 4]
Data Items of lst2: [0, 1, 'spam', 3, 4]
[/code]

जैसाकि इस Example के Output में हम देख सकते हैं कि जब हम val1 को val2 में Assign करने के बाद val1 को नए Object से Set करते हैं, तब val2 के Output में कोई Change नहीं होता, लेकिन जब हम lst1 को lst2 में Assign करने के बाद lst1 में नए Object को Index Number 2 पर Assign करते हैं, तब lst2 के Output में भी Change हो जाता है।

ऐसा इसीलिए होता है क्‍योंकि List जैसे Mutable Object के Reference Hold करने वाले Variable में Change करने पर In-Place Change होता है यानी Variable जिस Object के Reference को Hold कर रहा है, उस Actual Object में Change हो जाता है। परिणामस्‍वरूप उस एक Object के Reference को जितने भी Variables Share कर रहे होते हैं, उन सभी के Output में परिवर्तन हो जाता है।

In-Place Change का ये व्‍यवहार केवल केवल Mutable Objects के साथ ही होता है और ये व्‍यवहार उसी तरह से होता है, जिस तरह से हम चाहते हैं लेकिन हमें हमेंशा इस बात को ध्‍यान रखना चाहिए कि Mutable Objects के साथ Shared References किस तरह से Deal करते हैं, ताकि हम गलती से भी कोई Bug Create न कर दें।

हालांकि यदि हम चाहें तो Python के इस Behavior को रोक सकते हैं और Python के इस Behavior को रोकने का यही तरीका है कि हम अपने Mutable Object को Copy कर लें और अपने Operations को Copy किए गए Object पर Apply करें, न कि Actual Object के Shared Reference पर। Object को Copy करने के लिए हम Built-In List Function को भी Use कर सकते हैं और Python की Standard Library के copy Module को भी Use कर सकते हैं।

Copy करने का जो Most Common तरीका Use किया जा सकता है, वो ये है कि हम Actual Mutable Object को Start to End तक Slice कर लें, जिसके परिणामस्‍वरूप हमें एक नया Object मिल जाएगा और Object के Reference को हम नए Variable में Hold कर सकते हैं। जैसे-

[code]
FileName: ListCopy.py
lst1 = [0, 1, 2, 3, 4]

lst2 = lst1[:]

print("Data Items of lst1:", lst1)
print("Data Items of lst2:", lst2)
print("Change Value of lst1's Index Number 2 to 'spam'")
lst2[2] = 'spam'

print()

print("After Adding 'spam' at lst1's Index Number 2")
print("Data Items of lst1:", lst1)
print("Data Items of lst2:", lst2)

Output
Data Items of lst1: [0, 1, 2, 3, 4]
Data Items of lst2: [0, 1, 2, 3, 4]
Change Value of lst1's Index Number 2 to 'spam'
After Adding 'spam' at lst1's Index Number 2
Data Items of lst1: [0, 1, 2, 3, 4]
Data Items of lst2: [0, 1, 'spam', 3, 4]
[/code]

और जैसाकि इस Output में हम देख सकते हैं कि इस बार lst1 व lst2 दोनों Same Reference Sharing नहीं कर रहे हैं, बल्कि हमने Slicing तरीके का प्रयोग करते हुए lst1 की एक Copy Create करके lst2 में उस Copy का Reference Store कर दिया है। इसीलिए जब इस Example में हमने lst2 के Index Number 2 पर Change किया, तो उसका कोई प्रभाव lst1 के Data Items पर नहीं पड़ा।

हालांकि Copy करने का ये Slicing वाला ये तरीका List पर ठीक से काम करेगा क्‍योंकि List एक प्रकार का Sequence है लेकिन Mutable Core Types जैसे कि Dictionary या Sets पर Copy का ये तरीका काम नहीं करेगा क्‍योंकि ये Sequence नहीं हैं। इसलिए List की तरह ही किसी Dictionary या Set Object को Copy करने के लिए हम उनके copy() Method को Use कर सकते हैं अथवा Original Object को इनके Type Names को dict या set में Parameter की तरह Pass कर सकते हैं

इसके साथ ही Standard Python Library का copy() Method Use कर सकते हैं जो कि Python के copy Module में Defined है और Nested Object Structure को Copy करने के लिए deepcopy() method को Use कर सकते हैं जो कि उस स्थिति में उपयोगी होता है, जब हम ऐसी Dictionary को Copy करना चाहते हैं, जिसके अन्‍दर एक List Object Nested है।

इस copy Method को Use करते हुए हम उपरोक्‍त Example को ही निम्‍नानुसार तरीके से Recreate कर सकते हैं, जो कि ज्‍यादा Generic तरीका है-

[code]
FileName: SequenceCopyMethod.py
import copy
lst1 = [0, 1, 2, 3, 4]
lst2 = copy.copy(lst1)              # Copy

print("Data Items of lst1:", lst1)
print("Data Items of lst2:", lst2)
print("Change Value of lst1's Index Number 2 to 'spam'")
lst2[2] = 'spam'
print()
print("After Adding 'spam' at lst1's Index Number 2")
print("Data Items of lst1:", lst1)
print("Data Items of lst2:", lst2)

Output
Data Items of lst1: [0, 1, 2, 3, 4]
Data Items of lst2: [0, 1, 2, 3, 4]
Change Value of lst1's Index Number 2 to 'spam'
After Adding 'spam' at lst1's Index Number 2
Data Items of lst1: [0, 1, 2, 3, 4]
Data Items of lst2: [0, 1, 'spam', 3, 4]
[/code]

Python in Hindi - BccFalna.comये Article इस वेबसाईट पर Selling हेतु उपलब्‍ध EBook Python in Hindi से लिया गया है। इसलिए यदि ये Article आपके लिए उपयोगी है, तो निश्चित रूप से ये EBook भी आपके लिए काफी उपयोगी साबित होगी।

Python in Hindi | Page: 602 | Format: PDF

BUY NOW GET DEMO REVIEWS