[Python-ideas] Compact repr operator (i.e., __short_repr__)

Cory Benfield cory at lukasa.co.uk
Thu Feb 11 04:00:42 EST 2016


> On 10 Feb 2016, at 19:31, Mahmoud Hashemi <mahmoud at hatnote.com> wrote:
> 
> For those examples, the pprint/reprlib behavior probably wouldn't be much different than dict/list of tuples. I can put my vote in, but I prefer to defer design specifics to the creators/maintainers of those types. That's my point.
> 
> Right now I feel like repr() is a powerful, but underleveraged function in the builtin ecosystem. Instead of pprint.pformat(), why not add a keyword argument, and have repr(obj, pretty=True), and have that call through with arguments to a more full featured __repr__? Same could go for repr(obj, length=150, depth=2). These could have defaults set for REPL sessions.
> 
> Everyone is doing hacky, incomplete heuristic introspection, from IPython to the standard library. Instead, let's embrace the complexities of viewing application state, and allow for formalization of community efforts.

If we want to embrace the complexities of viewing application state, perhaps we should divorce this conversation from the repr altogether.

There’s plenty of applications where “viewing application state” will require more than the repr generally provides: you’ll want insight into private member variables, into the state of composed objects, and potentially into the state of some of those private member variables (e.g. is this lock held at this moment?). This is outside the generally accepted scope of the repr as I understand it: most people don’t provide the repr in this form.

If you want the complexity of application state then you probably want some way to dump the graph of objects starting with a specific one, along with their state. That’s totally beyond the scope of the repr, and is closer to what pickle would do for you (though as Andrew points out, pickle is not the right tool for this job either).

What I don’t understand is: if you are in the REPL already, what does any of this buy you? The REPL has plenty of powerful tools for introspecting Python objects: specifically, it has the Python programming language! If you want to introspect application state from the REPL, then just grab the object you want and start interrogating its state. dir() works, printing fields works, calling methods works, and all of this is far more powerful than anything you can get from the repr.

Cory

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160211/bed43c39/attachment-0001.sig>


More information about the Python-ideas mailing list