Python Dunder Methods __repr__ e __str__
This commit is contained in:
19
aula147.py
19
aula147.py
@@ -16,25 +16,22 @@
|
||||
# __str__(self) - str
|
||||
# __repr__(self) - str
|
||||
class Ponto:
|
||||
def __init__(self, x, y, outra='Nada'):
|
||||
def __init__(self, x, y, z='String'):
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.outra = outra
|
||||
self.z = z
|
||||
|
||||
def __str__(self):
|
||||
return f'Ponto - {self.x}, {self.y}'
|
||||
return f'({self.x}, {self.y})'
|
||||
|
||||
def __repr__(self):
|
||||
return f'Ponto(x={self.x!r}, y={self.y!r}, outra={self.outra!r})'
|
||||
# class_name = self.__class__.__name__
|
||||
class_name = type(self).__name__
|
||||
return f'{class_name}(x={self.x!r}, y={self.y!r}, z={self.z!r})'
|
||||
|
||||
|
||||
p1 = Ponto(1, 2)
|
||||
p2 = Ponto(90, 43)
|
||||
p2_str = str(p2)
|
||||
p2 = Ponto(978, 876)
|
||||
print(p1)
|
||||
print(p2)
|
||||
print()
|
||||
print(repr(p1))
|
||||
print(repr(p2))
|
||||
print()
|
||||
print(f'{p1!r}')
|
||||
print(f'{p2!r}')
|
||||
|
||||
Reference in New Issue
Block a user