Here is an example:
def __str__(self):
return f'({self.self.country},{self.self.code},{self.self.product}, {self.cost},{self.quantity })'
This way, when you assign values to a class, you can print its string representation
print(new_shoe)
More info here https://www.pythontutorial.net/python-oop/python-str/
Answer from ssanga on Stack Overflowpython - How to use __str__ function to return a string representation of a class - Stack Overflow
What does __str__ method do?
python - What is the purpose of __str__ and __repr__? - Stack Overflow
string - __str__ method in Python - Stack Overflow
I'm still a beginner in python and my English is not really good, so I may have a little bit of understanding the explanation given in my course.
So I have this code:
class Apple:
def __init__ (self,color,flavor):
self.color=color
self.flavor=flavor
def __str__(self):
return "This apple is {} and its flavor is {}".format(self.name,self.flavor)
jonagold=Apple("red","sweet")
print(jonagold)The only purpose I see in using the __str__ method, I don't have to write the method in the print () line. If I changed the __str__ method with:
def say(self):
return "This apple is {} and its flavor is {}".format(self.name,self.flavor)I would just need to type
print(jonagold.say())
And I would get the same answer. What are the other purpose of using __str__? In what situation would I need to use the __str__?
Thank you :)
__repr__
Called by the
repr()built-in function and by string conversions (reverse quotes) to compute the "official" string representation of an object. If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment).
__str__
Called by the
str()built-in function and by the print statement to compute the "informal" string representation of an object.
Use __str__ if you have a class, and you'll want an informative/informal output, whenever you use this object as part of string. E.g. you can define __str__ methods for Django models, which then gets rendered in the Django administration interface. Instead of something like <Model object> you'll get like first and last name of a person, the name and date of an event, etc.
__repr__ and __str__ are similar, in fact sometimes equal (Example from BaseSet class in sets.py from the standard library):
def __repr__(self):
"""Return string representation of a set.
This looks like 'Set([<list of elements>])'.
"""
return self._repr()
# __str__ is the same as __repr__
__str__ = __repr__
The one place where you use them both a lot is in an interactive session. If you print an object then its __str__ method will get called, whereas if you just use an object by itself then its __repr__ is shown:
>>> from decimal import Decimal
>>> a = Decimal(1.25)
>>> print(a)
1.25 <---- this is from __str__
>>> a
Decimal('1.25') <---- this is from __repr__
The __str__ is intended to be as human-readable as possible, whereas the __repr__ should aim to be something that could be used to recreate the object, although it often won't be exactly how it was created, as in this case.
It's also not unusual for both __str__ and __repr__ to return the same value (certainly for built-in types).
Hangman.__str__(hangman)
This line will just call the __str__ method. So does this btw. which is the preferred way to do it (in general, don’t call special methods directly):
str(hangman)
str and the __str__ method are just there to convert the object into a string, but not to print it. For example you could just as well log it to a file, so printing wouldn’t always be appropriate.
Instead, if you want to print it, just print it:
print(hangman)
print will automatically call str() on the object and as such use the type’s __str__ method to convert it to a string.
Hangman.__str__(hangman) isn't a command to print the string representation of hangman, it's an expression that evaluates to the string representation of hangman.
If you type that manually at the interactive prompt, you'll get the value of the expression printed, because the interactive prompt does that as a convenience. Having that line in a script (or in a function you call) will not print anything - you need to actually tell python to print it, with print(hangman).