__str__
Printing one of our objects shows something like <__main__.Planet object at 0x7f3a...>: its class and where it sits in memory. Not very useful. A method named __str__ tells Python how the object should look as text
__str__ takes only self and returns a string. print() calls it by itself
class Planet:
def __init__(self, name, moons):
self.name = name
self.moons = moons
def __str__(self):
return f"{self.name} with {self.moons} moons"
mars = Planet("Mars", 2)
print(mars)
Output
Mars with 2 moons
We don't call mars.__str__() ourselves. Python does it whenever the object has to become text
str() and f-strings use __str__ too, so the object fits right into any text
class Planet:
def __init__(self, name, moons):
self.name = name
self.moons = moons
def __str__(self):
return f"{self.name} with {self.moons} moons"
earth = Planet("Earth", 1)
text = str(earth)
print(text.upper())
print(f"Home: {earth}")
Output
EARTH WITH 1 MOONS
Home: Earth with 1 moons
__str__ can choose its words, like any method. Here a planet without moons says so
class Planet:
def __init__(self, name, moons):
self.name = name
self.moons = moons
def __str__(self):
if self.moons == 0:
return f"{self.name}, no moons"
return f"{self.name}, moons: {self.moons}"
for planet in [Planet("Venus", 0), Planet("Mars", 2)]:
print(planet)
Output
Venus, no moons
Mars, moons: 2
A list of objects is printed with a different method, __repr__, meant for programmers. Without one, a printed list shows the memory addresses again. The easy way is to print the items one by one, or join their texts
class Planet:
def __init__(self, name):
self.name = name
def __str__(self):
return self.name
planets = [Planet("Venus"), Planet("Mars")]
print(" | ".join(str(p) for p in planets))
Output
Venus | Mars
Mission: Planet Cards
The station's school shows a card for each planet. Each card is one line of text, made by the planet object itself
- Write a class Planet with an
__init__that takes aname, a number ofmoonsandrings(True or False), and a__str__that returnsname, moons: N, with, ringsadded at the end when the planet has rings. A planet with 0 moons saysname, no moonsinstead ofmoons: 0 - Start your program with three variables: names, moons and rings, three lists in the same order. For example
names = ["Saturn", "Mars", "Venus"],moons = [146, 2, 0]andrings = [True, False, False] - Make a
Planetfor each, and print each one withprint(), giving it the object itself - Print
With rings:and the cards of the planets with rings joined with/with a space on each side, ornone
The lists have from 1 to 10 planets, with 0 to 200 moons each
Example for the values above
Saturn, moons: 146, rings
Mars, moons: 2
Venus, no moons
With rings: Saturn, moons: 146, rings