Skip to content

Commit 49109db

Browse files
committed
fixes #633
1 parent 2164542 commit 49109db

File tree

2 files changed

+73
-23
lines changed

2 files changed

+73
-23
lines changed

fastcore/basics.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ def basic_repr(flds=None):
4949
flds = list(flds or [])
5050
def _f(self):
5151
res = f'{type(self).__module__}.{type(self).__name__}'
52-
if not flds: return f'<{res}>'
53-
sig = ', '.join(f'{o}={getattr(self,o)!r}' for o in flds)
52+
fs = flds if flds else [o for o in vars(self) if not o.startswith('_')]
53+
sig = ', '.join(f'{o}={getattr(self,o)!r}' for o in fs)
5454
return f'{res}({sig})'
5555
return _f
5656

nbs/01_basics.ipynb

+71-21
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": null,
5+
"execution_count": 1,
66
"metadata": {},
77
"outputs": [],
88
"source": [
@@ -11,7 +11,7 @@
1111
},
1212
{
1313
"cell_type": "code",
14-
"execution_count": null,
14+
"execution_count": 2,
1515
"metadata": {},
1616
"outputs": [],
1717
"source": [
@@ -26,7 +26,7 @@
2626
},
2727
{
2828
"cell_type": "code",
29-
"execution_count": null,
29+
"execution_count": 3,
3030
"metadata": {},
3131
"outputs": [],
3232
"source": [
@@ -55,7 +55,7 @@
5555
},
5656
{
5757
"cell_type": "code",
58-
"execution_count": null,
58+
"execution_count": 4,
5959
"metadata": {},
6060
"outputs": [],
6161
"source": [
@@ -65,7 +65,7 @@
6565
},
6666
{
6767
"cell_type": "code",
68-
"execution_count": null,
68+
"execution_count": 5,
6969
"metadata": {},
7070
"outputs": [],
7171
"source": [
@@ -84,7 +84,7 @@
8484
},
8585
{
8686
"cell_type": "code",
87-
"execution_count": null,
87+
"execution_count": 6,
8888
"metadata": {},
8989
"outputs": [],
9090
"source": [
@@ -94,7 +94,7 @@
9494
},
9595
{
9696
"cell_type": "code",
97-
"execution_count": null,
97+
"execution_count": 7,
9898
"metadata": {},
9999
"outputs": [],
100100
"source": [
@@ -113,7 +113,7 @@
113113
},
114114
{
115115
"cell_type": "code",
116-
"execution_count": null,
116+
"execution_count": 8,
117117
"metadata": {},
118118
"outputs": [],
119119
"source": [
@@ -125,7 +125,7 @@
125125
},
126126
{
127127
"cell_type": "code",
128-
"execution_count": null,
128+
"execution_count": 32,
129129
"metadata": {},
130130
"outputs": [],
131131
"source": [
@@ -136,8 +136,8 @@
136136
" flds = list(flds or [])\n",
137137
" def _f(self):\n",
138138
" res = f'{type(self).__module__}.{type(self).__name__}'\n",
139-
" if not flds: return f'<{res}>'\n",
140-
" sig = ', '.join(f'{o}={getattr(self,o)!r}' for o in flds)\n",
139+
" fs = flds if flds else [o for o in vars(self) if not o.startswith('_')]\n",
140+
" sig = ', '.join(f'{o}={getattr(self,o)!r}' for o in fs)\n",
141141
" return f'{res}({sig})'\n",
142142
" return _f"
143143
]
@@ -151,16 +151,16 @@
151151
},
152152
{
153153
"cell_type": "code",
154-
"execution_count": null,
154+
"execution_count": 33,
155155
"metadata": {},
156156
"outputs": [
157157
{
158158
"data": {
159159
"text/plain": [
160-
"'<__main__.SomeClass>'"
160+
"'__main__.SomeClass()'"
161161
]
162162
},
163-
"execution_count": null,
163+
"execution_count": 33,
164164
"metadata": {},
165165
"output_type": "execute_result"
166166
}
@@ -179,7 +179,7 @@
179179
},
180180
{
181181
"cell_type": "code",
182-
"execution_count": null,
182+
"execution_count": 34,
183183
"metadata": {},
184184
"outputs": [
185185
{
@@ -188,7 +188,7 @@
188188
"\"__main__.SomeClass(a=1, b='foo')\""
189189
]
190190
},
191-
"execution_count": null,
191+
"execution_count": 34,
192192
"metadata": {},
193193
"output_type": "execute_result"
194194
}
@@ -203,9 +203,16 @@
203203
"repr(SomeClass())"
204204
]
205205
},
206+
{
207+
"cell_type": "markdown",
208+
"metadata": {},
209+
"source": [
210+
"Nested objects work too:"
211+
]
212+
},
206213
{
207214
"cell_type": "code",
208-
"execution_count": null,
215+
"execution_count": 35,
209216
"metadata": {},
210217
"outputs": [
211218
{
@@ -214,7 +221,7 @@
214221
"\"__main__.AnotherClass(c=__main__.SomeClass(a=1, b='foo'), d='bar')\""
215222
]
216223
},
217-
"execution_count": null,
224+
"execution_count": 35,
218225
"metadata": {},
219226
"output_type": "execute_result"
220227
}
@@ -228,9 +235,40 @@
228235
"repr(AnotherClass())"
229236
]
230237
},
238+
{
239+
"cell_type": "markdown",
240+
"metadata": {},
241+
"source": [
242+
"Instance variables (but not class variables) are shown if `basic_repr` is called with no arguments:"
243+
]
244+
},
231245
{
232246
"cell_type": "code",
233-
"execution_count": null,
247+
"execution_count": 36,
248+
"metadata": {},
249+
"outputs": [
250+
{
251+
"data": {
252+
"text/plain": [
253+
"\"__main__.SomeClass(a=1, b='foo')\""
254+
]
255+
},
256+
"execution_count": 36,
257+
"metadata": {},
258+
"output_type": "execute_result"
259+
}
260+
],
261+
"source": [
262+
"class SomeClass:\n",
263+
" def __init__(self, a=1, b='foo'): self.a,self.b = a,b\n",
264+
" __repr__=basic_repr()\n",
265+
"\n",
266+
"repr(SomeClass())"
267+
]
268+
},
269+
{
270+
"cell_type": "code",
271+
"execution_count": 37,
234272
"metadata": {},
235273
"outputs": [],
236274
"source": [
@@ -6661,7 +6699,7 @@
66616699
},
66626700
{
66636701
"cell_type": "code",
6664-
"execution_count": null,
6702+
"execution_count": 38,
66656703
"metadata": {},
66666704
"outputs": [],
66676705
"source": [
@@ -6682,9 +6720,21 @@
66826720
"split_at_heading": true
66836721
},
66846722
"kernelspec": {
6685-
"display_name": "python3",
6723+
"display_name": "Python 3 (ipykernel)",
66866724
"language": "python",
66876725
"name": "python3"
6726+
},
6727+
"language_info": {
6728+
"codemirror_mode": {
6729+
"name": "ipython",
6730+
"version": 3
6731+
},
6732+
"file_extension": ".py",
6733+
"mimetype": "text/x-python",
6734+
"name": "python",
6735+
"nbconvert_exporter": "python",
6736+
"pygments_lexer": "ipython3",
6737+
"version": "3.11.8"
66886738
}
66896739
},
66906740
"nbformat": 4,

0 commit comments

Comments
 (0)