getitem & setitem dunder methods in Python | custom class (list, dict) | make own container #kody_az
Dərs haqqında
__getitem__ və __setitem__ Python-da xüsusi (dunder) metodlardır və obyektlərin [] kvadrat mötərizə ilə işləməsinə imkan yaradır.
__getitem__ obyektin üzərindən dəyər oxunarkən çağırılır, məsələn obj[key] yazanda Python avtomatik bu metodu işlədir.
__setitem__ isə obyektin daxilində dəyər yazmaq və ya dəyişmək üçün istifadə olunur, yəni obj[key] = value yazanda işə düşür.
Bu metodlar sayəsində öz class-ını list və ya dictionary kimi istifadə etmək mümkündür.
Əgər yalnız __getitem__ yazılsa, obyekt yalnız oxuna bilən olacaq və dəyişiklik etmək mümkün olmayacaq.
__setitem__ də əlavə edildikdə obyekt mutable, yəni dəyişdirilə bilən hala gəlir.
---------
__getitem__
• Bu xüsusi (dunder) metod obyektin üzərində dəyər oxuma əməliyyatı aparanda çağırılır.
• Yəni obj[key] yazanda, Python daxildə obj.__getitem__(key) metodunu işlədir.
• Adətən list, dict, tuple kimi daxili container-lərin davranışını öz class-larında təkrarlamaq üçün istifadə olunur.
• Əgər bu metod müəyyən edilməyibsə və obj[key] işlədilsə, Python TypeError xətası qaytaracaq.
__setitem__
• Bu metod obyektin üzərinə dəyər yazma və ya dəyişmə əməliyyatı aparanda çağırılır.
• Yəni obj[key] = value yazanda, Python daxildə obj.__setitem__(key, value) metodunu çağırır.
• Bunun vasitəsilə istədiyin açarın necə yadda saxlanılacağını və ya dəyişdiriləcəyini idarə edə bilərsən.
• Əgər bu metod yazılmayıbsa, obj[key] = value işlədildikdə TypeError çıxacaq.
========
__getitem__ and __setitem__ are special (dunder) methods in Python that allow objects to work with square brackets [].
__getitem__ is called when reading a value from an object, for example, when you write obj[key], Python automatically invokes this method.
__setitem__ is used to write or update a value inside the object, meaning when you write obj[key] = value, it is triggered.
Thanks to these methods, you can make your own class behave like a list or dictionary.
If only __getitem__ is defined, the object will be read-only and cannot be modified.
When __setitem__ is also added, the object becomes mutable, i.e., changeable.
⸻
__getitem__
• This special (dunder) method is called when performing a value lookup on an object.
• That means when you write obj[key], Python internally calls obj.__getitem__(key).
• It is commonly used to replicate the behavior of built-in containers like lists, dicts, and tuples in custom classes.
• If this method is not defined and obj[key] is used, Python will raise a TypeError.
__setitem__
• This method is called when assigning or updating a value in an object.
• That means when you write obj[key] = value, Python internally calls obj.__setitem__(key, value).
• With this method, you can control how a key should be stored or updated inside the object.
• If it is not defined, using obj[key] = value will result in a TypeError.
#python #kody_az #kodyaz #viral #fleet #jetbrains #pycharm #oop #viralcode #viralvideo #random #code #coder #azerbaycan #azerbaijan #azərbaycan #baku #baki #programming #programmer #codes #hazırlıq #hazirliq #kurs #kurslar #course #courses #online #onlinecourses #test #quiz