The difference between __str__ and __repr__?
I am new in Python. I write this code:
class Item:
def __init__(self, name):
self._name = name;
def __str__(self):
return "Item: %s" % self._name
when i run this line code:print((Item("Car"),)), the output is
(<__main__.Item object at 0x0000000002D32400>,). when i chage the code to
this:
class Item:
def __init__(self, name):
self._name = name;
def __repr__(self):
return "Item: %s" % self._name
def __str__(self):
return "Item: %s" % self._name
when i run this line code print((Item("Car"),)), the output is (Item: Car,).
so i am confused about __repr__ and __str__.
No comments:
Post a Comment