Python Dunder Methods __repr__ e __str__

This commit is contained in:
Luiz Otávio
2022-11-19 12:24:26 -03:00
parent c8d899194f
commit f553b2c592

View File

@@ -1,4 +1,4 @@
# Teoria: python Special Methods, Magic Methods ou Dunder Methods
# Python Dunder Methods __repr__ e __str__
# Dunder = Double Underscore = __dunder__
# Antigo e útil: https://rszalski.github.io/magicmethods/
# https://docs.python.org/3/reference/datamodel.html#specialnames
@@ -15,3 +15,26 @@
# __neg__(self) - -self
# __str__(self) - str
# __repr__(self) - str
class Ponto:
def __init__(self, x, y, outra='Nada'):
self.x = x
self.y = y
self.outra = outra
def __str__(self):
return f'Ponto - {self.x}, {self.y}'
def __repr__(self):
return f'Ponto(x={self.x!r}, y={self.y!r}, outra={self.outra!r})'
p1 = Ponto(1, 2)
p2 = Ponto(90, 43)
p2_str = str(p2)
print(p1)
print(p2)
print()
print(repr(p1))
print(repr(p2))
print()
print(f'{p1!r}')