repr example

This commit is contained in:
Will McGugan 2021-06-09 18:28:10 +01:00
parent 74ba2e1cdf
commit 932c451dd9
1 changed files with 9 additions and 6 deletions

View File

@ -1,3 +1,7 @@
from rich.repr import rich_repr
@rich_repr
class Bird:
def __init__(self, name, eats=None, fly=True, extinct=False):
self.name = name
@ -5,9 +9,6 @@ class Bird:
self.fly = fly
self.extinct = extinct
def __repr__(self):
return f"Bird({self.name!r}, eats={self.eats!r}, fly={self.fly!r}, extinct={self.extinct!r})"
def __rich_repr__(self):
yield self.name
yield "eats", self.eats
@ -15,11 +16,13 @@ class Bird:
yield "extinct", self.extinct, False
__rich_repr__.angular = True
from rich import print
from rich import print
BIRDS = {
"gull": Bird("gull", eats=["fish", "chips", "ice cream", "sausage rolls"]),
"penguin": Bird("penguin", eats=["fish"], fly=False),
"dodo": Bird("dodo", eats=["fruit"], fly=False, extinct=True)
"dodo": Bird("dodo", eats=["fruit"], fly=False, extinct=True),
}
print(BIRDS)