१. मानद्वारा पास गर्ने र सन्दर्भद्वारा पास गर्ने बीचको भिन्नता
Python मा, कार्यहरूलाई तर्कहरू पास गर्ने दुई तरिकाहरू छन्: मानद्वारा पास (pass-by-value) र सन्दर्भद्वारा पास (pass-by-reference)।
- Pass-by-value : मानको प्रतिलिपि कार्यमा तर्कको रूपमा पास गरिने विधि; कार्यभित्र पैरामीटर परिवर्तन गर्दा मूल चलमा असर पर्दैन।
- Pass-by-reference : चलको सन्दर्भ (ठेगाना) कार्यमा पास गरिने विधि, जसले कार्यभित्र गरिएका परिवर्तनहरू मूल चलमा प्रतिबिम्बित हुन्छ।
Python मा, यो व्यवहार वस्तुको प्रकृतिमा निर्भर गरी फरक पर्छ। Python को “pass-by-reference” व्यवहार विशेष गरी परिवर्तनशील (mutable) डेटा प्रकारहरूमा लागू हुन्छ र कोडको व्यवहारमा ठूलो प्रभाव पार्न सक्छ, त्यसैले यसलाई सही रूपमा बुझ्नु महत्त्वपूर्ण छ।
२. Python मा वस्तुहरूको विशेषताहरू
Python मा, सबै डेटा वस्तुहरू रूपमा व्यवहार गरिन्छ, र तिनीहरूको वस्तु विशेषताहरूको आधारमा तिनीहरूलाई अपरिवर्तनीय (immutable, परिवर्तन गर्न नसकिने) वा परिवर्तनीय (mutable, परिवर्तन गर्न सकिने) रूपमा वर्गीकृत गरिन्छ। यो भेदले तिनीहरू कार्यहरूमा तर्कको रूपमा पास गर्दा कसरी व्यवहार गर्छन् भन्नेमा प्रभाव पार्छ।
- Immutable objects : सिर्जना पछि परिवर्तन गर्न नसकिने वस्तुहरू। उदाहरणहरूमा पूर्णांक (
int), फ्लोटिङ‑प्वाइन्ट नम्बर (float), स्ट्रिङ (str), ट्युपल (tuple) समावेश छन्। यी डेटा प्रकारहरू एक पटक सिर्जना भएपछि परिवर्तन गर्न नसक्ने भएकाले, कार्यभित्र तिनीहरूमा गरिएका अपरेसनहरूले सन्दर्भद्वारा पास गरिए पनि कलरलाई असर गर्दैन। - Mutable objects : सिर्जना पछि परिवर्तन गर्न सकिने वस्तुहरू। उदाहरणहरूमा सूची (
list), शब्दकोश (dict), सेट (set) समावेश छन्। कार्यभित्र यी डेटा प्रकारहरूमा गरिएका परिवर्तनहरू कलरमा प्रतिबिम्बित हुन्छन्, जसले Python को सन्दर्भ‑पास गर्ने व्यवहारले विशेष प्रभाव पार्छ।

३. Python ले तर्कहरू कसरी पास गर्छ
Python मा, जब तपाईं कार्यलाई तर्कहरू पास गर्नुहुन्छ, वस्तुहरूको सन्दर्भहरू पास गरिन्छ। यो व्यवहारलाई कहिलेकाहीँ “pass-by-object-reference” भनिन्छ। यहाँबाट, वस्तुहरू अपरिवर्तनीय हो या परिवर्तनशील, त्यसअनुसार Python को व्यवहार देखाउने ठोस उदाहरणहरू हेरौं।
३.१ अपरिवर्तनीय वस्तुहरू
जब तपाईं अपरिवर्तनीय वस्तु कार्यमा पास गर्नुहुन्छ, कार्यभित्र नयाँ मान असाइन गर्दा नयाँ वस्तु सिर्जना हुन्छ र मूल वस्तुमा असर पर्दैन।
उदाहरण: अपरिवर्तनीय पूर्णांक पास गर्दै
def modify_number(num):
num = num * 2
print("Number inside the function:", num)
original_num = 5
modify_number(original_num)
print("Number outside the function:", original_num)
Output:
Number inside the function: 10
Number outside the function: 5
Explanation
modify_number कार्यभित्र, चल num दोब्बर गरिन्छ, तर यसले कार्यबाहिरको original_num लाई असर गर्दैन। पूर्णांकहरू अपरिवर्तनीय भएकाले, नयाँ मान असाइन गर्दा नयाँ वस्तु सिर्जना हुन्छ, र original_num अपरिवर्तित रहन्छ।
३.२ परिवर्तनशील वस्तुहरू
अर्कोतर्फ, जब परिवर्तनशील वस्तु कार्यमा पास गरिन्छ, त्यसको सन्दर्भ कार्यभित्र पनि प्रयोग हुन्छ। परिणामस्वरूप, कार्यभित्र गरिएका परिवर्तनहरू मूल वस्तुमा पनि प्रतिबिम्बित हुन्छ।
उदाहरण: परिवर्तनशील सूची पास गर्दै
def add_item(my_list):
my_list.append("Added element")
print("List inside the function:", my_list)
original_list = ["Element 1", "Element 2"]
add_item(original_list)
print("List outside the function:", original_list)
Output:
List inside the function: ['Element 1', 'Element 2', 'Added element']
List outside the function: ['Element 1', 'Element 2', 'Added element']
Explanation
सूचीहरू परिवर्तनशील भएकाले, add_item कार्यभित्र गरिएका परिवर्तनहरू कार्यबाहिरको original_list मा सिधै प्रतिबिम्बित हुन्छ। यसरी, परिवर्तनशील वस्तुहरू कार्यभित्र र बाहिर साझा गरिन्छ, त्यसैले कार्यभित्रको परिमार्जनले कलरलाई पनि असर गर्छ।
४. सन्दर्भद्वारा पास गर्दा सावधानीहरू र उपायहरू
Passing mutable objects to functions in Python can lead to unexpected behavior. Here are some countermeasures to avoid such issues.
४.१ वस्तुहरूको प्रतिलिपि प्रयोग गर्नुहोस्
If you want to modify an object inside a function, you can avoid affecting the original by copying it before making changes. In Python, you can use the copy module to create shallow copies (copy.copy) or deep copies (copy.deepcopy).
Example: Using Deep Copy to Avoid Mutable Object Issues
import copy
def add_item(my_list):
my_list_copy = copy.deepcopy(my_list)
my_list_copy.append("Added element")
print("List inside the function (copy):", my_list_copy)
original_list = ["Element 1", "Element 2"]
add_item(original_list)
print("List outside the function:", original_list)
Output:
List inside the function (copy): ['Element 1', 'Element 2', 'Added element']
List outside the function: ['Element 1', 'Element 2']
व्याख्या
By using deepcopy, a new object is created inside the function, leaving the original list unaffected. This allows independent changes inside and outside the function.
४.२ डिफ़ॉल्ट आर्गुमेन्टको लागि None प्रयोग गर्नुहोस्
Avoid setting mutable objects as function default arguments; it’s recommended to use None as the default and create a new object inside the function.
Example: Safe Way to Make the Default Argument None
def add_item(my_list=None):
if my_list is None:
my_list = []
my_list.append("New element")
print("List inside the function:", my_list)
return my_list
# When no list is passed
result = add_item()
print("Returned list:", result)
# When a list is passed
existing_list = ["Element 1", "Element 2"]
result = add_item(existing_list)
print("Returned list:", result)
print("Original list:", existing_list)
Output:
List inside the function: ['New element']
Returned list: ['New element']
List inside the function: ['Element 1', 'Element 2', 'New element']
Returned list: ['Element 1', 'Element 2', 'New element']
Original list: ['Element 1', 'Element 2', 'New element']
व्याख्या
By using the default argument None and generating a new list inside the function, you can prevent external lists from being unexpectedly modified. Setting None as the default argument makes clear whether to create a new list or use the one provided.

५. व्यावहारिक उदाहरण: Pass-by-Reference को बुझाइ गहिरो बनाउने कोड
Let’s review what we’ve covered with a practical example.
Example: Modifying a Dictionary’s Keys and Values
Dictionaries are mutable objects, so operations performed inside a function affect the dictionary outside the function. The example below shows the behavior when changing a dictionary’s keys and values.
def modify_dict(my_dict):
my_dict["new key"] = "new value"
print("Dictionary inside the function:", my_dict)
original_dict = {"key1": "value1", "key2": "value2"}
modify_dict(original_dict)
print("Dictionary outside the function:", original_dict)
Output:
Dictionary inside the function: {'key1': 'value1', 'key2': 'value2', 'new key': 'new value'}
Dictionary outside the function: {'key1': 'value1', 'key2': 'value2', 'new key': 'new value'}
व्याख्या
Because dictionaries are mutable, additions made inside the function are also reflected in the original_dict outside the function. Like lists, dictionaries can affect the caller because they are passed by reference.
६. सारांश
Python को “pass-by-reference” वस्तुको विशेषताहरूमा निर्भर गरी फरक व्यवहार देखाउँछ, त्यसैले यसलाई बुझ्नु तपाईंको कोडको विश्वसनीयता सुधार्न आवश्यक छ। विशेष गरी, mutable वस्तुहरूलाई रेफरेन्स पास गर्दा अनपेक्षित व्यवहार टाल्नको लागि प्रतिलिपि बनाउने र डिफ़ॉल्ट आर्गुमेन्टको रूपमा None प्रयोग गर्ने सिफारिस गरिन्छ।
सारांश बुँदाहरू
- पास‑बाइ‑भ्यालु र पास‑बाइ‑रेफरेन्स बीचको भिन्नता : पायथनमा, कार्यभित्र गरिएको परिवर्तनले मूल चलमा असर गर्छ कि गर्दैन भन्ने वस्तुको प्रकृतिमा निर्भर गर्दछ।
- अपरिवर्तनीय बनाम परिवर्तनशील : पायथनका अपरिवर्तनीय वस्तुहरू कार्यभित्र गरिएका परिवर्तनहरूलाई प्रतिबिम्बित गर्दैनन्, जबकि परिवर्तनशील वस्तुहरू रेफरेन्स पासिङ मार्फत कार्यभित्र र बाहिर साझा गरिन्छन्।
- निवारण रणनीतिहरू : परिवर्तनशील वस्तुहरूको लागि,
copyमोड्युलकोdeepcopyप्रयोग गर्ने, वा डिफ़ॉल्ट आर्गुमेन्टलाईNoneसेट गर्ने जस्ता विधिहरू अनपेक्षित व्यवहारबाट बच्न प्रभावकारी हुन्छन्।
म आशा गर्छु कि यो लेखले पायथनमा रेफरेन्स पासिङको बारेमा तपाईंको बुझाइलाई गहिरो बनाएको छ। यो ज्ञानलाई प्रयोग गरी प्रभावकारी, कम त्रुटिपूर्ण कोड लेख्नुहोस्।



