Python Magic Methods | __str__ හා __repr__ | Ghost Blog SC
Unlocking Python's Magic: __str__ and __repr__ SC Guide
ආයුබෝවන් යාලුවනේ!
Python Programming කරන අපිට Object-Oriented Programming (OOP) කියන Concepts හරිම වැදගත් නේද? Classes හදනකොට, Objects එක්ක වැඩ කරනකොට සමහර වෙලාවට පොඩි "මැජික්" වගේ දේවල් වෙනවා. අද අපි කතා කරන්න යන්නේ Python වල තියෙන එහෙම "මැජික්" Methods දෙකක් ගැන: ඒ තමයි __str__
සහ __repr__
කියන "Dunder Methods" දෙක. මේවා හරියටම තේරුම් අරගෙන use කරන එක ඔයාලගේ code එක ලස්සනට, organize කරලා තියාගන්න, ඒ වගේම debugging කරනකොටත් ගොඩක් උදව් වෙනවා. එහෙනම් අපි බලමු මේවා මොනවද, කොහොමද වැඩ කරන්නේ කියලා.
මොනවද මේ Magic Methods (Dunder Methods) කියන්නේ?
Python වල තියෙන මේ __str__
සහ __repr__
වගේ Methods වලට අපි කියනවා Magic Methods (මැජික් Methods) නැත්නම් Dunder Methods කියලා. 'Dunder' කියන්නේ 'Double Underscore' කියන එකේ කෙටි නමක්. මේ Methods වල ආරම්භයේ සහ අවසානයේ Double Underscore (__
) දෙකක් තියෙනවා. උදාහරණයක් විදිහට __init__
method එක ගත්තොත්, අපි class එකක object එකක් හදනකොට automatic activate වෙනවා නේද? ඒ වගේම __add__
(object දෙකක් එකතු කරනකොට), __len__
(object එකක length එක බලනකොට) වගේ ගොඩක් Magic Methods තියෙනවා. මේ Methods වල ප්රධානම වැදගත්කම තමයි Python වල built-in operations වලට අපේ custom objects කොහොමද ප්රතිචාර දක්වන්නේ කියලා define කරන්න පුළුවන් වෙන එක. ඒ කියන්නේ, අපේ objects වලට 'custom behavior' එකක් දෙන්න පුළුවන්.
Object එකක් Print කරනකොට මොකද වෙන්නේ?
අපි මුලින්ම බලමු, අපි Class එකක් හදලා ඒකේ Object එකක් print කරන්න හැදුවොත් මොකද වෙන්නේ කියලා. අපි හිතමු Laptop එකක් ගැන විස්තර තියාගන්න පුංචි Laptop
Class එකක් හදනවා කියලා. මේකේ __str__
හෝ __repr__
Method එකක් නැහැ.
class Laptop:
def __init__(self, brand, model, price):
self.brand = brand
self.model = model
self.price = price
my_laptop = Laptop("HP", "Spectre x360", 180000)
print(my_laptop)
මේ code එක run කලාම ඔයාලට ලැබෙන output එක මේ වගේ එකක් වෙන්න පුළුවන්:
<__main__.Laptop object at 0x0000020A8D9F9D60>
දැක්කනේ? මේක අපිට තේරුම් ගන්න අමාරුයි. මේ Object එක මොකක්ද, ඒකේ තියෙන data මොනවද කියලා මේකෙන් හරියටම කියන්න බැහැ. Object එකේ Memory Location එක විතරයි මේකෙන් පෙන්නන්නේ. මේක තමයි අපේ ගැටලුව. මේකට විසඳුම තමයි __str__
සහ __repr__
Methods.
__str__
Method එක: User-friendly ව කියන්න
Theory:
__str__
Method එකේ ප්රධානම purpose එක තමයි Object එකක User-friendly, කියවන්න පහසු (readable) String Representation එකක් return කරන එක. ඒ කියන්නේ, මේ Object එක මොකක්ද කියලා end-user කෙනෙක්ට, නැත්නම් code එක නොදන්න කෙනෙක්ට වුණත් තේරෙන විදිහට ඉදිරිපත් කිරීම තමයි මේකෙන් කරන්නේ. මේ Method එක str()
function එකෙන්, print()
statement එකෙන්, ඒ වගේම f-strings ඇතුලේදී automatic activate වෙනවා. මේ Method එක අනිවාර්යයෙන්ම String එකක් return කරන්න ඕනේ.
Practice/Example:
අපි දැන් අපි කලින් හදපු Laptop
Class එකට __str__
Method එක එකතු කරමු.
class Laptop:
def __init__(self, brand, model, price):
self.brand = brand
self.model = model
self.price = price
def __str__(self):
return f"{self.brand} {self.model} (Rs. {self.price:,}.00)"
my_laptop = Laptop("Dell", "XPS 15", 250000)
print(my_laptop) # print() calls __str__
print(str(my_laptop)) # str() function calls __str__
print(f"My new laptop is: {my_laptop}") # f-string calls __str__
මේ code එක run කලාම ඔයාලට මේ වගේ output එකක් ලැබෙයි:
Dell XPS 15 (Rs. 250,000.00)
Dell XPS 15 (Rs. 250,000.00)
My new laptop is: Dell XPS 15 (Rs. 250,000.00)
දැන් දැක්කනේ, අපේ Laptop
Object එක print කරාම කලින් වගේ memory address එකක් නෙවෙයි, ඒ වෙනුවට user කෙනෙක්ට තේරෙන විදිහට 'Dell XPS 15 (Rs. 250,000.00)' කියලා ලස්සන String එකක් ලැබෙනවා. මේක තමයි __str__
Method එකේ බලය!
__repr__
Method එක: Developer-friendly ව කියන්න
Theory:
__repr__
Method එක කියන්නේ Object එකක "Unambiguous" (ගැටලුවක් නැතුව නිවැරදිව හඳුනාගන්න පුළුවන්) String Representation එකක් return කරන Method එකක්. මේක ප්රධාන වශයෙන්ම Developersලාට සහ Debugging කරනකොට තමයි ගොඩක් වැදගත් වෙන්නේ. __repr__
Method එකෙන් ලැබෙන String එක සාමාන්යයෙන් Object එක නැවත හදන්න පුළුවන් විදිහට (re-create the object) වෙන්න ඕනේ. ඒ කියන්නේ, ඔයාට පුළුවන් නම් eval(obj.__repr__())
වගේ දෙයක් කරලා original Object එකම ආයෙත් හදන්න, එහෙම String එකක් තමයි __repr__
එකෙන් දෙන්න ඕනේ. මේ Method එක repr()
function එකෙන් සහ interactive Python shell එකේදී Object එකක් direct type කරාම activate වෙනවා. මේකත් String එකක් return කරන්න ඕනේ.
Practice/Example:
අපි දැන් අපේ Laptop
Class එකට __repr__
Method එකත් එකතු කරමු. මේක __str__
එකට වඩා පොඩ්ඩක් වෙනස් විදිහට තමයි ලියන්නේ.
class Laptop:
def __init__(self, brand, model, price):
self.brand = brand
self.model = model
self.price = price
def __str__(self):
return f"{self.brand} {self.model} (Rs. {self.price:,}.00)"
def __repr__(self):
return f"Laptop('{self.brand}', '{self.model}', {self.price})"
my_laptop = Laptop("Asus", "ROG Zephyrus G14", 300000)
print(my_laptop) # Calls __str__
print(repr(my_laptop)) # Calls __repr__
# Interactive shell එකේදී වගේ direct object එක type කරාම (Console/Terminal එකේ):
# my_laptop
# Output: Laptop('Asus', 'ROG Zephyrus G14', 300000)
මේ code එක run කලාම ඔයාලට මේ වගේ output එකක් ලැබෙයි:
Asus ROG Zephyrus G14 (Rs. 300,000.00)
Laptop('Asus', 'ROG Zephyrus G14', 300000)
දැන් බලන්න! print(my_laptop)
එකෙන් user-friendly output එක ලැබුණත්, repr(my_laptop)
එකෙන් ලැබුණේ Laptop('Asus', 'ROG Zephyrus G14', 300000)
කියන String එක. මේක බැලුවහම Object එකේ type එක මොකක්ද, ඒක හදන්න මොන parameter ටිකද ඕනේ කියලා developer කෙනෙක්ට පැහැදිලිව තේරෙනවා. Debugging කරනකොට මේක හරිම වැදගත්.
__str__
සහ __repr__
අතර වෙනස සහ Best Practices
දැන් ඔයාලා __str__
සහ __repr__
Method දෙකේම වැඩේ මොකක්ද කියලා තේරුම් ගත්තා නේද? මේ දෙක අතර තියෙන ප්රධාන වෙනස්කම් සහ මේවා use කරන්න ඕනෙ කොහොමද කියලා අපි සරලව බලමු.
ප්රධාන වෙනස්කම් (Key Differences):
__str__
:- Purpose: User-friendly, readable output. End-usersට, logs වලට, හෝ print කරන්න සුදුසුයි.
- Formality: Informal representation.
- Called by:
print()
,str()
, f-strings. - Example Output:
Dell XPS 15 (Rs. 250,000.00)
__repr__
:- Purpose: Unambiguous, developer-friendly output. Debugging වලට, interactive shell එකේදී Object එකක් examine කරන්න සුදුසුයි. Object එක re-create කරන්න පුළුවන් විදිහට තිබීම හොඳයි.
- Formality: Formal representation.
- Called by:
repr()
, interactive Python shell එකේදී Object එක direct type කරාම. - Example Output:
Laptop('Dell', 'XPS 15', 250000)
කවදද මොකක්ද Use කරන්නේ? (When to use which?)
Python community එකේ තියෙන Best Practice එක තමයි හැමවිටම __repr__
Method එක Implement කරන එක. ඒකට හේතුව තමයි, __str__
Method එක Define කරලා නැත්නම්, print()
සහ str()
Methods වලින් automatic activate වෙන්නේ __repr__
Method එකයි. ඒ කියන්නේ __repr__
එක __str__
එකට fallback එකක් (backup එකක්) විදිහට වැඩ කරනවා.
ඉතින්, ඔයාට විශේෂයෙන්ම User-friendly String එකක් ඕනේ නම් විතරක් __str__
Method එකත් Implement කරන්න. හැබැයි හැම Class එකකටම __repr__
Method එක අනිවාර්යයෙන්ම තියෙන්න ඕනේ කියලා තමයි කියන්නේ. මොකද මේක Debugging වලට හරිම වැදගත්.
හිතන්න, ඔයාලා Python interactive shell එකේ වැඩ කරනවා කියලා. එතකොට Object එකක් define කරලා ඒකේ නම ගහලා Enter කරාම output වෙන්නේ ඒ Object එකේ __repr__
String එක. මේකෙන් ඔයාලට ඒ Object එකේ තත්වෙ (state) මොකක්ද කියලා ඉක්මනට තේරුම් ගන්න පුළුවන්.
Troubleshooting/Debugging Benefits:
ඔයාලගේ Code එකේ Bug එකක් ආවොත්, Object එකක current state එක print කරලා බලන්න මේ Methods හරිම වැදගත්. __repr__
එකෙන් Object එකේ Exact State එක දැනගන්න පුළුවන් නිසා, 'මේ Object එක හැදුනේ හරියටද?', 'මේකේ Values ටික හරියට set වෙලාද?' වගේ දේවල් බලන්න හරිම ලේසියි.
අවසාන වශයෙන් (In Conclusion)
Python වල __str__
සහ __repr__
Magic Methods ගැන මේ විස්තර ඔයාලට පැහැදිලි වෙන්න ඇති කියලා හිතනවා. මුලින් මේවා පොඩ්ඩක් සංකීර්ණ වගේ පෙනුනත්, මේවා තේරුම් අරගෙන use කරන එක ඔයාලගේ Python Programming skill එක තවත් වැඩි කරනවා. ඒ වගේම ඔයාලගේ Code එක professional විදිහට maintain කරන්නත්, Debugging process එක පහසු කරන්නත් මේ Methods ගොඩක් උදව් වෙනවා.
ඔයාලත් අදම ඔයාලගේ Classes වලට මේ Methods එකතු කරලා බලන්න. ඒ වගේම මේ ගැන ඔයාලගේ අදහස්, ප්රශ්න හෝ තවත් මේ වගේ "Magic Methods" ගැන දැනගන්න කැමති නම්, පහලින් තියෙන Comment Section එකේ කියන් යන්න.
එහෙනම්, තවත් අලුත් Technical Article එකකින් හමුවෙමු!
සුබ දවසක්!