logo

Inplace לעומת אופרטורים סטנדרטיים ב-Python

מפעילים במקום - סט 1 סט 2
מפעילים רגילים עושים את עבודת ההקצאה הפשוטה. מצד שני אופרטורים של Inplace מתנהגים באופן דומה לאופרטורים רגילים אֶלָא שהם פועלים בצורה שונה במקרה של מטרות משתנות ובלתי ניתנות לשינוי. 
 

java int להכפיל
  • ה _לְהוֹסִיף_ השיטה עושה חיבור פשוט לוקח שני ארגומנטים מחזירה את הסכום ומאחסנת אותו במשתנה אחר מבלי לשנות אף אחד מהארגומנטים.
  • מִצַד שֵׁנִי _יאדד_ השיטה גם לוקחת שני ארגומנטים אבל היא מבצעת שינוי במקום בארגומנט הראשון שהועבר על ידי אחסון הסכום בו. כמו מוטציה של אובייקט יש צורך בתהליך זה מטרות בלתי ניתנות לשינוי כגון מחרוזות מספרים וטפולים לא אמורה להיות שיטת _iadd_ .
  • 'add()' של האופרטור הרגילהשיטה מיישמת ' א+ב ' ומאחסן את התוצאה במשתנה שהוזכר.התקן 'iadd()' של האופרטורהשיטה מיישמת ' a+=b ' אם הוא קיים (כלומר במקרה של יעדים בלתי ניתנים לשינוי הוא לא קיים) ומשנה את הערך של הארגומנט שעבר. אֲבָל אם לא 'a+b' מיושם .


מקרה 1 : יעדים בלתי ניתנים לשינוי.  
במטרות בלתי ניתנות לשינוי כגון מחרוזות מספרים וטופלים. אופרטורים של Inplace מתנהגים כמו אופרטורים רגילים, כלומר רק ההקצאה מתרחשת לא מתבצע שינוי בארגומנטים שעברו.
 

Python
# Python code to demonstrate difference between  # Inplace and Normal operators in Immutable Targets # importing operator to handle operator operations import operator # Initializing values x = 5 y = 6 a = 5 b = 6 # using add() to add the arguments passed  z = operator.add(ab) # using iadd() to add the arguments passed  p = operator.iadd(xy) # printing the modified value print ('Value after adding using normal operator : 'end='') print (z) # printing the modified value print ('Value after adding using Inplace operator : 'end='') print (p) # printing value of first argument # value is unchanged print ('Value of first argument using normal operator : 'end='') print (a) # printing value of first argument # value is unchanged print ('Value of first argument using Inplace operator : 'end='') print (x) 

תְפוּקָה:



Value after adding using normal operator : 11 Value after adding using Inplace operator : 11 Value of first argument using normal operator : 5 Value of first argument using Inplace operator : 5


מקרה 2 : יעדים הניתנים לשינוי  
ההתנהגות של אופרטורים של Inplace במטרות הניתנות לשינוי כגון רשימות ומילונים שונה מאופרטורים רגילים. ה עדכון והקצאה שניהם מתבצעים במקרה של מטרות משתנות.
 

Python
# Python code to demonstrate difference between  # Inplace and Normal operators in mutable Targets # importing operator to handle operator operations import operator # Initializing list a = [1 2 4 5] # using add() to add the arguments passed  z = operator.add(a[1 2 3]) # printing the modified value print ('Value after adding using normal operator : 'end='') print (z) # printing value of first argument # value is unchanged print ('Value of first argument using normal operator : 'end='') print (a) # using iadd() to add the arguments passed  # performs a+=[1 2 3] p = operator.iadd(a[1 2 3]) # printing the modified value print ('Value after adding using Inplace operator : 'end='') print (p) # printing value of first argument # value is changed print ('Value of first argument using Inplace operator : 'end='') print (a) 

תְפוּקָה: 
 

Value after adding using normal operator : [1 2 4 5 1 2 3] Value of first argument using normal operator : [1 2 4 5] Value after adding using Inplace operator : [1 2 4 5 1 2 3] Value of first argument using Inplace operator : [1 2 4 5 1 2 3]


 

צור חידון