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 it carefully enough.
```

The import should happen in the same scope. Modifying the global namespace could be confusing.
You're right again.  It could still break code like this:

from pprint import pprint
...
def myfunc():
    # Code modified to use !p here
    pprint(something) # Oops, pprint is now a module, not a function

unless the import could actually be local to the f-string itself, not the surrounding scope (perhaps similar to the way a list comprehension is implemented as a function with its own scope).

I don't totally understand these matters, so I may have got something wrong.

You don't need to go through globals, locals, etc. You can do the equivalent of:

>>> import pprint
>>> import sys
>>> pprint = 3
>>> sys.modules['pprint'].pprint('hello')
'hello'

Except that "sys" wouldn't be a local/global, either. But of course you could still monkeypatch pprint and break this code. There are a million ways to break code, I wouldn't worry about how the interpreter would get access to the pprint module. Instead, I'd worry about the more troubling issues which I raised in an earlier email. Those are the ones that are likely to stop this proposal.

Eric
_______________________________________________
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/TBSKYG5PULPIQGZYA5AEHVJEQUZMV7JU/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to