[Python-ideas] cli tool to print value, similar to pydoc

Thomas Gläßle t_glaessle at gmx.de
Mon Apr 8 07:00:15 EDT 2019



Steven D'Aprano wrote on 4/8/19 4:35 AM:
> How will it know what object os is, without guessing, if you haven't 
> imported it?

Like pydoc/help does it. I assume it splits by dots and imports the
longest importable subsplit (try...except ImportError in a loop), then
iteratively getattrs the rest.

I admit that there is a little ambiguity here in that you could also
import the shortest possible subsplit such that the rest of the name can
be resolved (which is definitely not what help does). In practice it
wouldn't be different in most cases, but if you're concerned about the
ambiguity, one could separate module and attribute part by colon, like
with entry points, e.g. "os:pathsep".

> I always have at least one REPL open for precisely this sort of thing, 
> and the interactive interpreter is infinitely more flexible and powerful 
> than a tool to print one value.

Sure, but I find that most uses are covered by either pydoc, or printing
a value, and more complex snippets are often better tried by putting it
into a script file - which makes it easier to make small modifications
and then re-execute.

Furthermore, often a single REPL is not enough, since you may want to
see the value in different python interpreters or environments.

In any case, it is surely not "needed" per se, but merely convenience.
Same applies for the default-usage of pydoc, and still I tend pydoc a
lot over opening a REPL and calling help(), because it combines two or
three steps into one.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 963 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190408/a2241b12/attachment-0001.sig>


More information about the Python-ideas mailing list