---
title: "Adding __rich__ methods to python classes"
description: "Adding a method that returns a rich renderable to any python class makes it display this output if printed with rich. This also includes being nested inside..."
date: 2021-12-23
published: false
tags:
  - python
  - rich
template: til
---


Adding a `__render__` method that returns a rich renderable to any python class
makes it display this output if printed with rich.  This also includes being
nested inside a rich Layout.


``` python
import rich
from rich.panel import Panel


class ShowMe:
    def __rich__(self):
        return Panel("hello", border_style="gold1")


if __name__ == "__main__":
    rich.print(ShowMe())
```

![results of printing ShowMe with rich](https://dropper.waylonwalker.com/file/e260b7fe-4977-4858-af88-c9ad67186014.webp)
