Attributes and Methods

Attributes vs. Methods (ပစ္စည်း vs. အလုပ်)

Attributes

ဒါက Object တစ်ခု ရဲ့ Data (အချက်အလက်) တွေ။

Person တစ်ယောက်မှာ name (နာမည်) ရှိတယ်။ age (အသက်) ရှိတယ်။ height (အရပ်) ရှိတယ်။ ဒါတွေက Attributes။

Methods

ဒါက Object တစ်ခု ဘာလုပ်နိုင်လဲ ဆိုတာကိုပြောတာ။ ဒါက Class ထဲမှာ ရေးထားတဲ့ Function တွေပဲ။

Person တစ်ယောက်က walk() (လမ်းလျှောက်) နိုင်တယ်။ talk() (စကားပြော) နိုင်တယ်။ sleep() (အိပ်) နိုင်တယ်။ ဒါတွေက Methods။

အက္ခရာကျ: အဲ့ဒီ Attributes တွေ Methods တွေကို သုံးချင်ရင် (access/use)၊ Object ရဲ့ နာမည်နောက်မှာ dot (.) ခံပြီး ခေါ်ရတယ်။
Accessing Attributes and Methods
# Attribute ကို ယူတာ my_person.name # Method ကို ခိုင်းတာ (function မို့လို့ ကွင်းစကွင်းပိတ် () ပါတယ်) my_person.talk()

Vibe နှစ်မျိုးရှိတယ် (Instance vs. Class)

ဒါက နည်းနည်း အာရုံနောက် (confusing) နိုင်တဲ့ နေရာ။

Attributes မှာရော၊ Methods မှာရော Vibe နှစ်မျိုး ရှိတယ်။

  • Instance Vibe (တစ်ယောက်ချင်းစီနဲ့ ဆိုင်တာ)
  • Class Vibe (အဖွဲ့လိုက်/အားလုံးနဲ့ ဆိုင်တာ)

1. Instance Attributes & Methods (The self Vibe)

ဒါက bro အမြဲတမ်းနီးပါး သုံးနေရမှာ။ ဒါက Object တစ်ခုချင်းစီနဲ့ပဲ သက်ဆိုင်တယ်။

  • __init__ ထဲမှာ self နဲ့ တွဲပြီး သတ်မှတ်ခဲ့တဲ့ ဟာတွေ အကုန်လုံးဟာ Instance Attributes တွေပဲ။
  • self ကို parameter အနေနဲ့ တောင်းတဲ့ Method တွေ အကုန်လုံးဟာ Instance Methods တွေပဲ။
Instance Attributes and Methods
class Person: def __init__(self, name, age): # 1. Instance Attributes # "self" (ဒီ Object) ရဲ့ name self.name = name # "self" (ဒီ Object) ရဲ့ age self.age = age # 2. Instance Method # "self" ကို သုံးထားတယ် def say_hello(self): # 'self.name' ကို ခေါ်သုံးထားတယ် print(f"ကျနော့်နာမည် {self.name} ပါ။ ကျနော်က {self.age} နှစ်။") # Object တွေ မွေးမယ် p1 = Person("Kyaw Kyaw", 25) p2 = Person("Su Su", 30)

Scene ကို ကြည့်:

  • p1.name က "Kyaw Kyaw"p1.age က 25
  • p2.name က "Su Su"p2.age က 30
  • name နဲ့ age ဆိုတာ p1 မှာ တစ်မျိုး၊ p2 မှာ တစ်မျိုး။ တစ်ယောက်ချင်းစီ အတွက် သီးသန့် ရှိတာ။ ဒါကို Instance Attributes လို့ခေါ်တယ်။

p1.say_hello() လို့ ခေါ်လိုက်ရင်... self နေရာမှာ p1 ဝင်သွားပြီး ကျနော့်နာမည် Kyaw Kyaw ပါ လို့ ထွက်မယ်။ p2.say_hello() လို့ ခေါ်လိုက်ရင်... self နေရာမှာ p2 ဝင်သွားပြီး ကျနော့်နာမည် Su Su ပါ လို့ ထွက်မယ်။

အဓိပ္ပာယ်: say_hello() method က self (Object ကိုယ်တိုင်) ရဲ့ data ကို ယူသုံးတယ်။ ဒါကို Instance Method လို့ခေါ်တယ်။

2. Class Attributes & Methods (The Group Vibe)

ဒါက Object တစ်ခုချင်းစီ အတွက် မဟုတ်ဘူး။ အဲ့ဒီ Class (ပုံစံခွက်) တစ်ခုလုံးနဲ့ ဆိုင်တာ။ အဲ့ Class ကနေ မွေး လိုက်တဲ့ Object တွေ အားလုံး Share တဲ့ ပစ္စည်းတွေ၊ အလုပ်တွေ။

  • Class Attribute တွေကို __init__ အပြင်ဘက်မှာ၊ class အောက်မှာ ရေးတယ်။
  • Class Method တွေကို def အပေါ်မှာ @classmethod ဆိုတဲ့ တံဆိပ် (decorator) ကပ်ပေးရတယ်။

ဥပမာ: Person class ကို Upgrade လုပ်မယ်။

Class Attributes and Methods
class Person: # 1. Class Attribute # "Person" ဆိုတဲ့ Class ကြီးတစ်ခုလုံးနဲ့ ဆိုင်တယ် # Person တိုင်းက "Human ပဲလေ species = "Homo Sapiens" def __init__(self, name, age): # Instance Attributes (အရင်တိုင်း) self.name = name self.age = age # Instance Method (အရင်တိုင်း) def say_hello(self): print(f"ကျနော့်နာမည် {self.name} ပါ။ ကျနော်က {self.age} နှစ်။") # 2. Class Method # 'self' မဟုတ်တော့ဘူး 'cls' (Class ကို ညွှန်းတာ) @classmethod def get_species(cls): print(f"ကျနော်တို့မျိုးစိတ်က {cls.species} ပါ။") @classmethod def create_baby(cls, name): # Class method ကိုသုံးပြီး Object အသစ် မွေးလို့ရတယ် # cls(...) ဆိုတာ Person(...) လို့ခေါ်တာနဲ့ အတူတူပဲ return cls(name, 0) # အသက် 0 နဲ့ မွေးမယ်

Scene ကို ကြည့်:

Using Class Attributes
p1 = Person("Kyaw Kyaw", 25) p2 = Person("Su Su", 30) # 1. Class Attribute ကို သုံးမယ် # Object ကနေ ခေါ်လို့ရတယ် print(f"Kyaw Kyaw ရဲ့ မျိုးစိတ်: {p1.species}") # "Homo Sapiens" print(f"Su Su ရဲ့ မျိုးစိတ်: {p2.species}") # "Homo Sapiens" # Class (ပုံစံခွက်) ကြီးကနေ တိုက်ရိုက်ခေါ်လို့လည်း ရတယ် print(f"Person class ရဲ့ မျိုးစိတ်: {Person.species}") # "Homo Sapiens"
Modifying Class Attributes
# 2. ဂေါ်တဲ့ အချက် # Class Attribute ကို Class ကနေ တိုက်ရိုက် ပြောင်းလိုက်ရင်... Person.species = "ALIEN" # Object တွေဆီမှာ auto ပြောင်းသွားတယ် print(f"Kyaw Kyaw (after change): {p1.species}") # "ALIEN" print(f"Su Su (after change): {p2.species}") # "ALIEN"
ရလား? species ဆိုတာ p1 တစ်ယောက်တည်း ပိုင်တာ မဟုတ်ဘူး၊ p2 တစ်ယောက်တည်း ပိုင်တာ မဟုတ်ဘူး။ Person အဖွဲ့ဝင် အားလုံး Share သုံးတဲ့ Group Data။
Using Class Methods
# 3. Class Method ကို သုံးမယ် # Class ကြီးကနေ တိုက်ရိုက်ခေါ်တယ် Person.get_species() # "ကျနော်တို့မျိုးစိတ်က ALIEN ပါ။" # Object ကနေလည်း ခေါ်လို့ရပေမယ့်၊ အသုံးနည်းတယ် p1.get_species() # "ကျနော်တို့မျိုးစိတ်က ALIEN ပါ။" # 'Factory' method ကို သုံးကြည့်မယ် baby = Person.create_baby("Baby Shark") print(f"Baby's name: {baby.name}") # "Baby Shark" print(f"Baby's age: {baby.age}") # 0 baby.say_hello()

create_baby method က Object တစ်ခု (self) ကို မလိုဘူး။ သူက Person Class (cls) ကိုပဲ လိုတယ်။ ဟေ့ Person class၊ ခင်ဗျားရဲ့ ပုံစံခွက်ကို သုံးပြီး Baby တစ်ယောက် ဖန်တီးပေး လို့ ခိုင်းတာ။

အနှစ်ချုပ်

Instance Vibe (The self Vibe)

  • Attributes: self.name (တစ်ယောက်ချင်းစီရဲ့ ပိုင်ဆိုင်မှု)
  • Methods: def do_something(self): (တစ်ယောက်ချင်းစီရဲ့ data ကို ကလိ တဲ့ အလုပ်)
  • Keywords: self

Class Vibe (The Group Vibe)

  • Attributes: species = "Human" (အဖွဲ့လိုက် Share သုံးတဲ့ ပိုင်ဆိုင်မှု)
  • Methods: def do_group_stuff(cls): (အဖွဲ့လိုက် data ကို ကလိ တဲ့ အလုပ်)
  • Keywords: @classmethod, cls
The end: bro တို့ အခု စရေးရင်တော့ 95% လောက်က Instance (self) Vibe တွေနဲ့ပဲ အလုပ်လုပ်နေရမှာ။ Class (Group) Vibe ကိုတော့ အော်... အဲ့လို Share သုံးလို့ရတဲ့ ဟာမျိုး ရှိတယ် လို့ သိထားရင်လန်းတာပေါ့။