(Jan-13-2020, 07:21 PM)AlluminumFoil Wrote: would you mind explaining? and how would I add that to my code?
inventory = []
class Items:
def __init__(self, item):
self.item = item
def __str__(self):
return f'The item is: {self.item}'
def __repr__(self):
return f'Items({self.item!r})'
Use:
>>> sword = Items('sword')
>>>
>>> # Call __str__
>>> print(sword)
The item is: sword
>>>
>>> # Call __repr__
>>> sword
Items('sword')
This is a standard way to use it.
__str__
here can add text that give more info about the object.
__repr__
Give raw info about object intended more as a aid for developers and debugging.