[Python-ideas] Re: add !p to pprint.pformat() in str.format() an f-strings

2020-07-25 Thread Serhiy Storchaka
16.07.20 07:34, Charles Machalow пише: Right now in str.format(), we have !s, !r, and !a to allow us to call str(), repr(), and ascii() respectively on the given expression. I'm proposing that we add a !p conversion to have pprint.pformat() be called to convert the given expression to a

[Python-ideas] Re: add !p to pprint.pformat() in str.format() an f-strings

2020-07-24 Thread Jelle Zijlstra
El mar., 21 jul. 2020 a las 17:27, Guido van Rossum () escribió: > A philosophical problem with this is proposal is that it takes a notation > that is processed by the bytecode compiler and makes it dependent on user > code to be imported from the stdlib. We only do that in rare cases — IIRC >

[Python-ideas] Re: add !p to pprint.pformat() in str.format() an f-strings

2020-07-21 Thread Eric V. Smith
On 7/21/2020 7:36 PM, Rob Cliffe wrote: On 21/07/2020 21:00, Eric V. Smith wrote: f-strings call PyObject_Repr directly, without going through builtins. If we added !p as described here, we'd need to call import every time we execute !p, because we don't know if it's been imported yet. At

[Python-ideas] Re: add !p to pprint.pformat() in str.format() an f-strings

2020-07-21 Thread Guido van Rossum
A philosophical problem with this is proposal is that it takes a notation that is processed by the bytecode compiler and makes it dependent on user code to be imported from the stdlib. We only do that in rare cases — IIRC the only other case is ‘import’ calling ‘__import__()’. This reversal of

[Python-ideas] Re: add !p to pprint.pformat() in str.format() an f-strings

2020-07-21 Thread Rob Cliffe via Python-ideas
On 21/07/2020 21:00, Eric V. Smith wrote: On 7/21/2020 2:54 PM, Alex Hall wrote: It should do the import for you. As was proposed: ``` print(f"My dict: {d!p}") ``` should be equivalent to ``` import pprint print(f"My dict: {pprint.pformat(d)}") ``` The import should happen in the same

[Python-ideas] Re: add !p to pprint.pformat() in str.format() an f-strings

2020-07-21 Thread Eric V. Smith
On 7/21/2020 4:59 PM, Rob Cliffe via Python-ideas wrote: On 21/07/2020 19:54, Alex Hall wrote: It should do the import for you. As was proposed: ``` print(f"My dict: {d!p}") ``` should be equivalent to ``` import pprint print(f"My dict: {pprint.pformat(d)}") You're right, I didn't read

[Python-ideas] Re: add !p to pprint.pformat() in str.format() an f-strings

2020-07-21 Thread Rob Cliffe via Python-ideas
On 21/07/2020 19:54, Alex Hall wrote: It should do the import for you. As was proposed: ``` print(f"My dict: {d!p}") ``` should be equivalent to ``` import pprint print(f"My dict: {pprint.pformat(d)}") You're right, I didn't read it carefully enough. ``` The import should happen in the

[Python-ideas] Re: add !p to pprint.pformat() in str.format() an f-strings

2020-07-21 Thread Eric V. Smith
On 7/21/2020 2:54 PM, Alex Hall wrote: It should do the import for you. As was proposed: ``` print(f"My dict: {d!p}") ``` should be equivalent to ``` import pprint print(f"My dict: {pprint.pformat(d)}") ``` The import should happen in the same scope. Modifying the global namespace could be

[Python-ideas] Re: add !p to pprint.pformat() in str.format() an f-strings

2020-07-21 Thread Alex Hall
It should do the import for you. As was proposed: ``` print(f"My dict: {d!p}") ``` should be equivalent to ``` import pprint print(f"My dict: {pprint.pformat(d)}") ``` The import should happen in the same scope. Modifying the global namespace could be confusing. A quick test shows that adding

[Python-ideas] Re: add !p to pprint.pformat() in str.format() an f-strings

2020-07-21 Thread Rob Cliffe via Python-ideas
That seems like a nice idea, but what would happen if pprint had not been imported?  NameError? Rob Cliffe On 16/07/2020 05:34, Charles Machalow wrote: Right now in str.format(), we have !s, !r, and !a to allow us to call str(), repr(), and ascii() respectively on the given expression. I'm

[Python-ideas] Re: add !p to pprint.pformat() in str.format() an f-strings

2020-07-16 Thread Alex Hall
+1. I imagine I would use this fairly often, particularly in debugging or interactive sessions. It goes well with the magic `=` in f-strings, e.g. `print(f"{d=!p}")`. It's more useful than the others because the manual way requires an import. It's easy to learn, easy to remember, and fits