Python

Operator overloading in Python (__add__)

Në shqip: Mbingarkimi i operatorëve në Python

ShpjegimiSQ

Me metodat speciale si __add__, __eq__ dhe __lt__, objektet e tua punojnë me +, == dhe <. Kështu dy vektorë mund t'i mbledhësh si numra.

EnglishEN

With special methods like __add__, __eq__ and __lt__, your objects work with +, == and <. That way two vectors can be added like numbers.

Si ta mendosh

Si ta mësosh një robot që „+“ me frutat do të thotë „vendosi në të njëjtën shportë“.

Lexoje në anglisht

Like teaching a robot that “+” with fruit means “put them in the same basket”.

Shembull kodipython

class Pika:
    def __init__(self, x, y): self.x, self.y = x, y
    def __add__(self, o): return Pika(self.x + o.x, self.y + o.y)
p = Pika(1, 2) + Pika(3, 4)

Terma të lidhur