__iadd__(self, other) — +=.
__isub__(self, other) — -=.
__imul__(self, other) — *=.
__itruediv__(self, other) — /=.
__i2oordiv__(self, other) — //=.
__imod__(self, other) — %=.
__ipow__(self, other[, modulo]) — **=.
__ilshift__(self, other) — <<=.
__irshift__(self, other) — >>=.
__iand__(self, other) — &=.
__ixor__(self, other) — ^=.
__ior__(self, other) — |=.
__neg__(self) — unar -.
__pos__(self) — unar+.
__abs__(self) — modul(abs()).
__invert__(self) — inversiya (~).
__complex__(self) — complexga keltirish.
__int__(self) — int ga keltirish.
__float__(self) — float ga keltirish.
__round__(self[, n]) — yaxlitlash.
__enter__(self), __exit__(self, exc_type, exc_value, traceback) — kontekst menejerlarini amaliyotda qo‘llash.
Bu metodlardan ba’zilarini qaytadan tavsiflab berilgan ikki o‘lchovli vektor misolida ko‘rib chiqay-lik:
import math
class Vector2D:
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return 'Vector2D({} , {} )'.format(self.x, self.y)
def __str__(self):
return '({} , {} )'.format(self.x, self.y)
107
def __add__(self, other):
return Vector2D(self.x + other.x, self.y + other.y)
def __iadd__(self, other):
self.x += other.x
self.y += other.y
return self
def __sub__(self, other):
return Vector2D(self.x - other.x, self.y - other.y)
def __isub__(self, other):
self.x -= other.x
self.y -= other.y
return self
def __abs__(self):
return math.hypot(self.x, self.y)
def __bool__(self):
return self.x != 0 or self.y != 0
def __neg__(self):
return Vector2D(-self.x, -self.y)
5.0
False
108
Ta’kidlash joizki, maxsus metodlarni yuklash yaxshi amaliyot, ammo uni haddan ziyod qo‘lla-maslik kerak. Ulardan faqat kodingizni o‘qish va tushunishni osonlashtirsagina foydalanishni maslahat beramiz.
109
31-bob: Dekoratorlar
Pythonda dekoratorlar va ulardan amaliyotda foydalanish
Xo‘sh, bu nima? Dekoratorlar nima, ular qanday ishlashi haqida gaplashish uchun biz, eng avvalo, Pythonda funksiyalar ham obyekt sifatida qaralishini esga olishimiz lozim. Bundan chiqdi, ularni boshqa funksiyalardan chaqirish hamda argument sifatida uzatish ham mumkin.
Mana endi dekoratorlarga o‘tsak ham bo‘ladi. Umuman olganda, dekoratorlar – funksiya kodini o‘zgartirmagan holda u bajaradigan ishni o‘zgartirish imkonini beruvchi qobiq.
O‘z shaxsiy dekoratorimizni “qo‘lda” yaratamiz:
def my_shiny_new_decorator(function_to_decorate):
Dekorator o`zining tarkibida “qobiq” funksiyani tavsiflaydi.
U dekoratsiyalanayotgan funksiya atrofini o`raydi.
def the_wrapper_around_the_original_function():
print("Men – funksiyani chaqirilishidan oldin ishlaydigan funksiyaman") function_to_decorate() # Funksiyaning o`zi
print("Men esa, keyin ishga tushadigan kodman")
# Bu funksiyani qaytaraman
return the_wrapper_around_the_original_function
# Faraz qilaylik, bizda biz endi o`zgartirish niyatimiz bo`lmagan funksiya bor.
def stand_alone_function():
print("Men yolg`iz funksiyaman, sen meni o`zgartirmassan, axir?!")
# ya`ni, #dekoratorga uzatishimiz
# U boshlang`ich funksiyani bizga kerakli ixtiyoriy funksiyaga o`raydi va
foydalanishga tayyor bo`lgan yangisini qaytaradi.
stand_alone_function_decorated = my_shiny_new_decorator(stand_alone_function)
stand_alone_function_decorated()
Men – funksiyani chaqirilishidan oldin ishlaydigan funksiyaman
Men yolg`iz funksiyaman, sen meni o`zgartirmassan, axir?!
Men esa, keyin ishga tushadigan kodman
Balki siz endi har safar stand_alone_functionni chaqirishning o‘rniga stand _alone_function_ decoratedni chaqirishni istarsiz. Agar shuni xohlasangiz, buning uchun stand_alone_functionni qayta yozib chiqish kifoya.
stand_alone_function = my_shiny_new_decorator(stand_alone_function)
Men – funksiyani chaqirilishidan oldin ishlaydigan funksiyaman
Men yolg`iz funksiyaman, sen meni o`zgartirmassan, axir?!
Men esa, keyin ishga tushadigan kodman
110
Umuman olganda, mana shuning o‘zi decorator hisoblanadi. Dekoratorlar sintaksisidan foydalan-gan holda avvalgi misolni quyidagicha yozish mumkin:
def another_stand_alone_function():
print("Meni tinch qo'ying")
another_stand_alone_function()
Men – funksiyani chaqirilishidan oldin ishlaydigan funksiyaman
Meni tinch qo`ying.
Men esa, keyin ishga tushadigan kodman
Ya’ni Pythondagi dekoratorlar – ko‘rinish konstruksiyalari uchun sintaktik shakar hisoblanadi.
another_stand_alone_function = my_shiny_new_decorator(another_stand_alone_function)
Bunda bitta funksiya uchun bir necha dekoratorni qo‘llash mumkin, masalan:
def bread(func): def wrapper():
print() func() print("<\______/>")
return wrapper
def wrapper():
print("#pomidorlar#")
func()
print("~salat~")
return wrapper
def sandwich(food="—go'sht--"): print(food)
--go'sht--
sandwich = bread(ingredients(sandwich))
#pomidorlar#
--go`sht--
~salat~
<\______/>
Dekoratorlar sintaksisidan foydalangan holda quyidagilarni kiritamiz:
111
@ingredients
def sandwich(food="--go'sht--"): print(food)
#pomidorlar#
--go'sht--
~salat~
<\______/>
Bundan tashqari, yodingizda bo‘lsin, dekoratsiyalash tartibi ham muhim ahamiyatga ega. Avvalgi misol bilan solishtirib ko‘ring:
@bread
def sandwich(food="--go'sht--"): print(food)
#pomidorlar#
--go'sht--
<\______/>
~salat~
Do'stlaringiz bilan baham: |