From 49109db3340f0a6b28b506eb327609f6c25c5a1a Mon Sep 17 00:00:00 2001 From: Jeremy Howard Date: Fri, 4 Oct 2024 15:45:12 +1000 Subject: [PATCH] fixes #633 --- fastcore/basics.py | 4 +- nbs/01_basics.ipynb | 92 ++++++++++++++++++++++++++++++++++----------- 2 files changed, 73 insertions(+), 23 deletions(-) diff --git a/fastcore/basics.py b/fastcore/basics.py index 4adee302..94d5f07f 100644 --- a/fastcore/basics.py +++ b/fastcore/basics.py @@ -49,8 +49,8 @@ def basic_repr(flds=None): flds = list(flds or []) def _f(self): res = f'{type(self).__module__}.{type(self).__name__}' - if not flds: return f'<{res}>' - sig = ', '.join(f'{o}={getattr(self,o)!r}' for o in flds) + fs = flds if flds else [o for o in vars(self) if not o.startswith('_')] + sig = ', '.join(f'{o}={getattr(self,o)!r}' for o in fs) return f'{res}({sig})' return _f diff --git a/nbs/01_basics.ipynb b/nbs/01_basics.ipynb index 8e3a4f5a..e8ae8a5e 100644 --- a/nbs/01_basics.ipynb +++ b/nbs/01_basics.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -11,7 +11,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -26,7 +26,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -55,7 +55,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -65,7 +65,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -84,7 +84,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -94,7 +94,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ @@ -113,7 +113,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ @@ -125,7 +125,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 32, "metadata": {}, "outputs": [], "source": [ @@ -136,8 +136,8 @@ " flds = list(flds or [])\n", " def _f(self):\n", " res = f'{type(self).__module__}.{type(self).__name__}'\n", - " if not flds: return f'<{res}>'\n", - " sig = ', '.join(f'{o}={getattr(self,o)!r}' for o in flds)\n", + " fs = flds if flds else [o for o in vars(self) if not o.startswith('_')]\n", + " sig = ', '.join(f'{o}={getattr(self,o)!r}' for o in fs)\n", " return f'{res}({sig})'\n", " return _f" ] @@ -151,16 +151,16 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'<__main__.SomeClass>'" + "'__main__.SomeClass()'" ] }, - "execution_count": null, + "execution_count": 33, "metadata": {}, "output_type": "execute_result" } @@ -179,7 +179,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 34, "metadata": {}, "outputs": [ { @@ -188,7 +188,7 @@ "\"__main__.SomeClass(a=1, b='foo')\"" ] }, - "execution_count": null, + "execution_count": 34, "metadata": {}, "output_type": "execute_result" } @@ -203,9 +203,16 @@ "repr(SomeClass())" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Nested objects work too:" + ] + }, { "cell_type": "code", - "execution_count": null, + "execution_count": 35, "metadata": {}, "outputs": [ { @@ -214,7 +221,7 @@ "\"__main__.AnotherClass(c=__main__.SomeClass(a=1, b='foo'), d='bar')\"" ] }, - "execution_count": null, + "execution_count": 35, "metadata": {}, "output_type": "execute_result" } @@ -228,9 +235,40 @@ "repr(AnotherClass())" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Instance variables (but not class variables) are shown if `basic_repr` is called with no arguments:" + ] + }, { "cell_type": "code", - "execution_count": null, + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\"__main__.SomeClass(a=1, b='foo')\"" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "class SomeClass:\n", + " def __init__(self, a=1, b='foo'): self.a,self.b = a,b\n", + " __repr__=basic_repr()\n", + "\n", + "repr(SomeClass())" + ] + }, + { + "cell_type": "code", + "execution_count": 37, "metadata": {}, "outputs": [], "source": [ @@ -6661,7 +6699,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "metadata": {}, "outputs": [], "source": [ @@ -6682,9 +6720,21 @@ "split_at_heading": true }, "kernelspec": { - "display_name": "python3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.8" } }, "nbformat": 4,