عوامل التشغيل الداخلية - مجموعة 1 مجموعة 2
يقوم المشغلون العاديون بمهمة التعيين البسيطة. من ناحية أخرى، يتصرف مشغلو Inplace بشكل مشابه للمشغلين العاديين يستثني أنهم يتصرفون بطريقة مختلفة في حالة الأهداف القابلة للتغيير وغير القابلة للتغيير.
- ال _يضيف_ الطريقة التي تقوم بها عملية الإضافة البسيطة تأخذ وسيطتين وتعيد المجموع وتخزنه في متغير آخر دون تعديل أي من الوسيطات.
- على الجانب الآخر _iadd_ تأخذ الطريقة أيضًا وسيطتين ولكنها تُجري تغييرًا موضعيًا في الوسيطة الأولى التي تم تمريرها عن طريق تخزين المجموع فيها. نظرًا لأن هناك حاجة إلى طفرة الكائن في هذه العملية، فهي أهداف غير قابلة للتغيير مثل سلاسل الأرقام والصفوف لا ينبغي أن يكون لديك طريقة _iadd_ .
الحالة 1 : أهداف غير قابلة للتغيير.
في الأهداف غير القابلة للتغيير مثل سلاسل الأرقام والصفوف. تتصرف عوامل التشغيل Inplace بنفس طريقة العوامل العادية، أي أن التعيين فقط يحدث ولا يتم إجراء أي تعديل في الوسائط التي تم تمريرها.
# 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 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]