Hi Steven,

> Are you aware that dunder names are reserved for Python's use?

I wasn't aware it was explicitly discouraged, thanks for the link.

It seems to me that "__pretty__" (however it's implemented) seems a very
sensible name for a method used when pretty printing objects. If it's one
day implemented officially I think the approach I took is a good start, if
it's not, then what I've done won't conflict with anything. :-)

I'm sure I've broken lots of rules by just taking a punt at an
implementation rather than enduring weeks of argument-for-arguments-sake on
this mailing list, but the more I read it, the happier I am about that
decision.

> Perhaps I don't understand the context here, but that doesn't sound like
a general approach that would work very well.

I think it does work well; though I agree I didn't explain it very well
last night (indeed my rushed explanation was outright wrong in some
regards).

Let me explain more how my implementation of "__pretty__" works, taking an
example form devtool's tests
<https://github.com/samuelcolvin/python-devtools/blob/7482e87dcb2dab47884a7908211859e1af4e09e3/tests/test_custom_pretty.py#L4-L24>
:

class CustomCls:
    def __pretty__(self, fmt, **kwargs):
        yield 'Thing('
        yield 1
        for i in range(3):
            yield fmt(list(range(i)))
            yield ','
            yield 0
        yield -1
        yield ')'


debug(CustomCls())
>> test.py:13 <module>
    CustomCls(): Thing(
        [],
        [0],
        [0, 1],
    ) (CustomCls)

__pretty__ takes two keyword arguments: fmt - a function, and skip_exc - an
exception which can be raised to stop pretty printing.

It then yields either:

   - A string, in which case that string is just displayed
   - An int, which tells devtools to move to a new line and maybe change
   the indent/context. 1 means increase indent, -1 means reduce the indent and
   0 means just new line with no indent change
   - the value returned from fmt() which causes devtools to take care of
   displaying the argument passed to fmt(), including recursive display of
   sub-objects

That's it. It's very simple but it allows effectively arbitrarily complex
objects to be displayed. Especially since an object that wants to take care
of all display of itself can just do so and return a string.

All that fmt() actually does is mark an object as requiring devtools to
take care of its display, it is implemented as a function argument to
__pretty__ to avoid libraries that implement __pretty__ from needing
devtools as a requirement.

I hope that makes more sense and acts as a starting point for a more
productive conversation about an standard approach to pretty printing.

Samuel
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/V33TVHARI7RGQHZANX66KKQIB4JBGWQW/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to