Class & static methods

Python မှာ Method သုံးမျိုး ရှိတယ်

Class တွေ ဆောက်တဲ့အခါ Method (Function) သုံးမျိုး ရေးလို့ရတယ်။ တစ်ခုချင်းစီမှာ သူ့ရဲ့ အလုပ်နဲ့ သူ့ရဲ့ သုံးပုံ မတူကြဘူး။

  • Instance Methods: Object တစ်ခုချင်းစီရဲ့ data တွေကို သုံးတယ်
  • Class Methods: Class တစ်ခုလုံးနဲ့ ဆိုင်တဲ့ အလုပ်တွေ လုပ်တယ်
  • Static Methods: Class data တွေ မသုံးတဲ့ utility functions တွေ

1. Instance Methods

ဒါက Bro သိပြီးသား။ Function ရေးလိုက်တိုင်း ပထမဆုံး parameter မှာ self ထည့်လိုက်ရင် အဲ့ဒါ Instance Method ပဲ။

အလုပ်: Object တစ်ခုချင်းစီ (Instance) ရဲ့ Data တွေကို ယူသုံးမယ်၊ ပြင်မယ်။

သော့ချက်: self ပါရမယ်။

2. Class Methods (The Manager)

သူက Object တစ်ခုချင်းစီကို စိတ်မဝင်စားဘူး။ Class ကြီး တစ်ခုလုံး နဲ့ဆိုင်တဲ့ အလုပ်တွေကို လုပ်တယ်။

Logic: ငါက တစ်ယောက်ချင်းစီကို မကြည့်ဘူး၊ အဖွဲ့လိုက် (Class) ကိုပဲ ကြည့်တယ်

Syntax:

  • ခေါင်းပေါ်မှာ @classmethod ဆိုတဲ့ တံဆိပ် (Decorator) တပ်ပေးရတယ်။
  • Parameter မှာ self အစား cls (Class) ကို သုံးရတယ်။

အသုံးဝင်ပုံ:

အများအားဖြင့် Alternative Constructor (Factory Method) အနေနဲ့ သုံးကြတယ်။ ဆိုလိုတာက Object ဆောက်တဲ့အခါ နည်းလမ်းအသစ်နဲ့ ဆောက်ချင်ရင် သုံးတာ။

ဥပမာ - Pizza ဆိုင်

ရိုးရိုး Pizza မှာ toppings တွေ လိုက်ထည့်လို့ရသလို၊ Margherita ဆိုတာနဲ့ ဘာ toppings ပါပြီးသားဆိုပြီး တန်းမှာလို့ ရတယ်မလား။ အဲ့ဒီ Margherita ဆိုပြီး အသင့်လုပ်ပေးလိုက်တာက Class Method ပေါ့။

Class Method Example - Pizza Factory
class Pizza: def __init__(self, ingredients): self.ingredients = ingredients def __repr__(self): return f"Pizza with {self.ingredients}" # ဒါက Class Method (Factory လိုမျိုး သုံးသွားတာ) @classmethod def margherita(cls): # cls(...) ဆိုတာ Pizza(...) နဲ့ အတူတူပဲ return cls(['mozzarella', 'tomatoes']) @classmethod def prosciutto(cls): return cls(['mozzarella', 'tomatoes', 'ham']) # သုံးကြည့်မယ် my_pizza = Pizza(['cheese', 'chicken']) # ရိုးရိုး Instance method (init) cheese_pizza = Pizza.margherita() # Class method နဲ့ လှမ်းမှာလိုက်တာ print(cheese_pizza) # Output: Pizza with ['mozzarella', 'tomatoes']

3. Static Methods (The Helper)

သူက ဘိုသီဘိုသိုက်ကောင်။ Class ထဲမှာတော့ နေတယ်၊ ဒါပေမယ့် Class (cls) နဲ့လည်း မဆိုင်ဘူး၊ Instance (self) နဲ့လည်း မဆိုင်ဘူး။

Logic: ငါ့မှာ Logic ရှိတယ်၊ တွက်ချက်ပေးစရာရှိတာ တွက်ပေးမယ်။ ငါ့ကို Class data တွေ လာမပေးနဲ့၊ ငါမလိုဘူး

Syntax:

  • ခေါင်းပေါ်မှာ @staticmethod တပ်ရတယ်။
  • self ရော cls ရော ဘာမှ မပါဘူး။ ရိုးရိုး function အတိုင်းပဲ။

အသုံးဝင်ပုံ:

Utility function တွေ (ဥပမာ - ပေါင်းနှုတ်မြှောက်စား လုပ်တာ၊ စာကြောင်းစစ်တာ) အတွက် သုံးတယ်။

ဥပမာ - Calculator

Static Method Example - Math Helper
class MathHelper: @staticmethod def add(x, y): return x + y @staticmethod def is_even(num): return num % 2 == 0 # Object တောင် ဆောက်စရာ မလိုဘူး၊ တန်းခေါ်သုံးလို့ရတယ် print(MathHelper.add(5, 10)) # Output: 15 print(MathHelper.is_even(8)) # Output: True

The Battle: ဘယ်ကောင် ဘယ်ချိန်သုံး

အမျိုးအစား Parameter Decorator ဘယ်အချိန်သုံးမလဲ?
Instance Method self မလို Object တစ်ခုချင်းစီရဲ့ self.name, self.age တွေကို သုံးချင်ရင်/ပြင်ချင်ရင် သုံး။ 90% ဒါပဲ သုံးရမှာ။
Class Method cls @classmethod Class Variable တွေကို ပြင်ချင်ရင် သုံး။ မဟုတ်လည်း Object ဆောက်ဖို့ နည်းလမ်းအသစ် (Factory) ဖန်တီးချင်ရင် သုံး။
Static Method မပါ @staticmethod Class ထဲက data တွေကို လုံးဝ မထိဘဲ၊ သီးသန့် တွက်ချက်မှု (Utility) လုပ်ချင်ရင် သုံး။

Bro မှတ်ထားရမယ့် Logic

  • self ဆိုတာ: ငါ (Object တစ်ခုချင်းစီ)။
  • cls ဆိုတာ: ငါတို့မျိုးနွယ် (Class ကြီးတစ်ခုလုံး)။
  • Static ဆိုတာ: ပြင်ပလူ (Class ထဲမှာ နေပေမယ့် ဘာမှ ဝင်မစွက်ဖက်သူ)။

နိဂုံး

ဒီ classmethod နဲ့ staticmethod ကို ပိုင်သွားပြီဆိုရင် Bro ရဲ့ Code တွေက ပိုပြီး Clean ဖြစ်သွားလိမ့်မယ်။ တခြား Developer တွေက Bro ကုဒ်ကို ဖတ်တဲ့အခါ ဟာ... ဒီကောင် Decorators တွေဘာတွေနဲ့ ရေးတတ်တယ်၊ ကျွမ်းဘဲပဲ ဆိုပြီး ဖြစ်သွားမှာ!

Key:
  • Instance Methods: အသုံးအများဆုံး၊ self နဲ့ သုံးတယ်၊ Object data ကို အလုပ်လုပ်တယ်
  • Class Methods: @classmethod decorator နဲ့ သုံးတယ်၊ cls parameter ယူတယ်၊ Factory pattern အတွက် သုံးတယ်
  • Static Methods: @staticmethod decorator နဲ့ သုံးတယ်၊ self/cls မလိုဘူး၊ Utility functions အတွက် သုံးတယ်