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.
signature.asc
Description: OpenPGP digital signature
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/