I have defined this class:
class Point():
def __init__(self,x,y):
self.x = x
self.y = y
def __str__(self):
return "Point x: {0}, Point y: {1}".format(self.x, self.y)
What is the difference between the 2 cases print("Point",p1)
and print(p1)
:
p1 = Point(1,2)
print("Point",p1)
print(p1)
>>('Point', <__main__.Point instance at 0x00D96F80>)
>>Point x: 1, Point y: 2
The former is printing a tuple containing "Point"
and p1
; in this case __repr__()
will be used to generate the string for output instead of __str__()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With