Classes၊ Instances နှင့် Python 3.15

3.15.2

State နဲ့ behavior အတူထားဖို့ class သုံးပါ။ လက်ရှိ Python series ရဲ့ အပြောင်းအလဲတချို့ကိုလည်း ကြည့်ပါ။

ဒီစာမျက်နှာမှာ

Instance Attributes နှင့် Methods

__init__ က instance ကို စတင်ပြင်ဆင်ပါတယ်။ self က method ဆီ ပေးလိုက်တဲ့ instance ကို ညွှန်းပါတယ်။ Object တစ်ခုစီ ကွာမယ့် value တွေအတွက် instance attribute သုံးပါ။ Class attribute ကို class က ပိုင်ပြီး instance က နာမည်တူ attribute မသတ်မှတ်မချင်း မျှဝေသုံးပါတယ်။

class OrderLine:
    def __init__(self, quantity, unit_price):
        self.quantity = quantity
        self.unit_price = unit_price

    def total(self):
        return self.quantity * self.unit_price

print(OrderLine(3, 2.5).total())
နမူနာ ရလဒ်
7.5

Inheritance နှင့် Composition

Subclass က inherited behavior ကို override လုပ်နိုင်ပါတယ်။ super() က Python method resolution order အတိုင်း ဆက်ခေါ်ပါတယ်။ Order ထဲမှာ order line list ထားသလို object တစ်ခုထဲ နောက် object ထည့်သုံးချင်ရင် composition သုံးပါ။

Python 3.15 မှာ ပြောင်းလဲထားတာများ

Python 3.15 မှာ UTF-8 Mode ကို default ဖွင့်ထားပြီး explicit lazy import နဲ့ comprehension ထဲ unpack လုပ်တာလို language feature အသစ်တွေ ပါလာပါတယ်။ ပြင်ပဒေတာရဲ့ encoding ကို သိရင် အတိအကျ သတ်မှတ်တာ အသုံးဝင်ဆဲပါ။ ဒီဥပမာတွေမှာ သာမန် import နဲ့ comprehension သုံးထားပါတယ်။

တရားဝင် ကိုးကားချက်များ