ဖြေရှင်းထားသည်- Python __sub__ မှော်နည်းလမ်း

Python ရှိ __sub__ မှော်နည်းလမ်းကို argument နှစ်ခုယူသည့် function ကိုခေါ်ဆိုရန်အတွက် အသုံးပြုသော်လည်း ပထမ argument ကို ဒုတိယ argument ၏ class ၏ subclass တစ်ခုအဖြစ် အဓိပ္ပာယ်ဖွင့်ဆိုထားပါသည်။ အတန်းခွဲများသည် __sub__ မှော်နည်းလမ်းကို အကောင်အထည်မဖော်ပါက ၎င်းသည် မမျှော်လင့်ထားသော အပြုအမူများဆီသို့ ဦးတည်သွားနိုင်သည်။

The __sub__ magic method is used to implement the subtraction operator for objects. It is called when the - operator is used on two objects of the same type. The __sub__ method should return the result of the subtraction operation.

For example, if we have a class that represents a complex number, we could define the __sub__ method like this:

class ComplexNumber: def __init__(self, real, imaginary): self.real = real self.imaginary = imaginary def __sub__(self, other): return ComplexNumber(self.real - other.real, self.imaginary - other.imaginary)

Now we can use the - operator on two ComplexNumber objects:

c1 = ComplexNumber(1, 2) c2 = ComplexNumber(3, 4) c3 = c1 - c2 print(c3) # Prints ComplexNumber(real=-2, imaginary=-2)

Magic Method ဆိုတာ ဘာလဲ။

Python တွင်၊ magic method သည် ၎င်း၏အမည်ကို မသတ်မှတ်ဘဲ function တစ်ခုကို ခေါ်ရန်ခွင့်ပြုသည့် အထူးလုပ်ဆောင်ချက်အမျိုးအစားဖြစ်သည်။ ၎င်းသည် function ၏အမည်ကို ampersand (&) ဖြင့်ရှေ့ဆက်ခြင်းဖြင့်လုပ်ဆောင်သည်။

မျက်လှည့်နည်းလမ်းများစာရင်း

Python တွင် မှော်လုပ်နည်းများစွာ ရှိသည်။ ဤအရာများမှာ-

1. ကျပန်းတင်သွင်းခြင်း။
2. တင်သွင်းချိန်
3. သင်္ချာမှ sqrt, pi ကိုတင်သွင်းပါ။
4. datetime မှတင်သွင်းသည့်ရက်စွဲ၊အချိန်
5. အော်ပရေတာများမှ ပေါင်းထည့်၊ နုတ်၊ ပွား၊ ခွဲ တင်သွင်းပါ။
6. Functools မှ တစ်စိတ်တစ်ပိုင်းကို တင်သွင်းပါ။
7. စုဆောင်းမှုများမှတင်သွင်းသော deque

Related ရေးသားချက်များ:

a Comment ချန်ထား