[SciPy-Dev] Custom __repr__?

Andrew Nelson andyfaff at gmail.com
Thu May 13 22:57:24 EDT 2021


On Fri, 14 May 2021 at 12:42, Robert Kern <robert.kern at gmail.com> wrote:

>
> That would work for something like quick-and-dirty code generation, but it
> will look awful on the prompt, and that's where I would want our use cases
> geared towards. To me, the main use case for a (roughly)
> eval()able __repr__ is to allow users to copy-paste an Out[] cell for use
> elsewhere, not for mechanical eval()ing. It's not hard to fix to assemble
> the args and kwds into what users would naturally type; it's just not a
> one-liner.
>

Implementing __str__ and __repr__ would alleviate some awfulness (so long
as one used `print(obj)`). One could argue that the default position is not
all that useful anyway:

```
>>> norm(scale=[1,2], loc=[3, 4])
<scipy.stats._distn_infrastructure.rv_frozen at 0x7fba939cc580>
```

because indicating that it's an rv_frozen, we don't know anything else
about it. Rough __repr__ to allow copy-paste may be good enough, in most
cases that is also sufficient to mechanical eval(). Perhaps there would
have to be a no-promises, best effort, you get what you get approach, to
the output. The entire prefix would have to be used to help with the import:

```
>>> from scipy.stats import norm
>>> repr(norm(scale=1, loc=2))
scipy.stats.norm(scale=1, loc=2)
```
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/pipermail/scipy-dev/attachments/20210514/8f671690/attachment-0001.html>


More information about the SciPy-Dev mailing list