logo

Python'da Yerinde ve Standart Operatörler

Yerinde Operatörler - Set 1 2'yi ayarla
Normal operatörler basit atama işini yapar. Öte yandan Inplace operatörleri normal operatörlere benzer şekilde davranır hariç Değişken ve Değişmez hedefler durumunda farklı şekilde hareket ettiklerini. 
 

  • _eklemek_ yöntem basit toplama yapar, iki argüman alır, toplamı döndürür ve hiçbir argümanı değiştirmeden bunu başka bir değişkende saklar.
  • Diğer taraftan _iadd_ yöntem de iki argüman alır ancak toplamı depolayarak iletilen 1. argümanda yerinde değişiklik yapar. Bu süreçte nesne mutasyonuna ihtiyaç duyulduğundan sayı dizileri ve tuple'lar gibi değişmez hedefler _iadd_ yöntemine sahip olmamalıdır .
  • Normal operatörün 'add()'ıyöntem uygulanır' a+b ' ve sonucu belirtilen değişkende saklar.Operatörün 'iadd()' komutunu değiştirinyöntem uygulanır' a+=b ' eğer varsa (yani değişmez hedefler durumunda mevcut değilse) ve iletilen argümanın değerini değiştirir. Ancak değilse 'a+b' uygulanır .


Durum 1 : Değişmez Hedefler.  
Sayı dizileri ve tuple'lar gibi değişmez hedeflerde. Yerinde operatörler normal operatörlerle aynı şekilde davranır; yani yalnızca atama gerçekleşir, iletilen argümanlarda hiçbir değişiklik yapılmaz.
 

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) 

Çıkış:



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


Durum 2 : Değişken Hedefler  
Inplace operatörlerinin listeler ve sözlükler gibi değişken hedeflerdeki davranışı normal operatörlerden farklıdır. hem güncelleme hem de atama gerçekleştirilir Değişken hedefler durumunda.
 

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) 

Çıkış: 
 

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]


 

Test Oluştur