Python

__repr__ vs __str__

Në shqip përdoret i njëjti emër.

ShpjegimiSQ

__str__ e shfaq objektin bukur për përdoruesin (print), ndërsa __repr__ e shfaq për programuesin, me detaje — zakonisht si kod që e krijon objektin. Në terminal dhe në lista shfaqet __repr__.

EnglishEN

__str__ shows the object nicely for the user (print), while __repr__ shows it for the programmer, with detail — usually like the code that creates it. The terminal and lists show __repr__.

Si ta mendosh

__str__ është përshkrimi në vitrinë; __repr__ është etiketa teknike në pjesën e pasme.

Lexoje në anglisht

__str__ is the shop-window description; __repr__ is the technical label on the back.

Shembull kodipython

class Pika:
    def __init__(self, x, y): self.x, self.y = x, y
    def __repr__(self): return f"Pika({self.x}, {self.y})"
print([Pika(1, 2)])   # [Pika(1, 2)]

Terma të lidhur