1. Python tili imkoniyatlari Pythonni ko`chirish


isub__(self, other) - -=. __imul



Yüklə 2,19 Mb.
səhifə63/78
tarix19.12.2023
ölçüsü2,19 Mb.
#152124
1   ...   59   60   61   62   63   64   65   66   ...   78
1. Python tili imkoniyatlari Pythonni ko`chirish

__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 menedjerlarini amaliyotda qo`llash.
Bu metodlarning ba`zilarini ba`zi metodlarini qaytadan tavsiflab berilgan ikki o`lchovli vektor misolida ko`rib chiqaylik:

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)
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)
>>> x = Vector2D(3, 4)
>>> x
Vector2D(3, 4)
>>> print(x)
(3, 4)
>>> abs(x)
5.0
>>> y = Vector2D(5, 6)
>>> y
Vector2D(5, 6)
>>> x + y
Vector2D(8, 10)
>>> x - y
Vector2D(-2, -2)
>>> -x
Vector2D(-3, -4)
>>> x += y
>>> x
Vector2D(8, 10)
>>> bool(x)
True
>>> z = Vector2D(0, 0)
>>> bool(z)
False
>>> -z
Vector2D(0, 0)

Yakunida shuni aytib o`tish kerakki, maxsus metodlarni yuklash – bu yaxshi amaliyot bo`lib, lekin uni haddan tashqari ko`p qo`llash ham yaxshi emas. Ulardan foydalanish kodingizni o`qish va tusunishni osonlashtirishiga ishonchingiz komil bo`lgandagina foydalanishni maslahat beramiz.


31-bob


Yüklə 2,19 Mb.

Dostları ilə paylaş:
1   ...   59   60   61   62   63   64   65   66   ...   78




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə