strange(?) module import syntax
I've come across the following code in a Python script
fr开发者_运维知识库om pprint import pprint
why not simply import pprint
?
Unless the module pprint contains a function called pprint which is being aliased as pprint (surely, this must be the definition of madness?)
It does contain a function pprint, and that is exactly what's going on. I much prefer typing pprint, not pprint.pprint, or decimal.Decimal, or datetime.datetime.now() - wouldn't you?
Yes, the syntax is from module import functions
, so the first pprint is the module name and the second the function name.
Your belief is correct, but it is not "aliased" in any way. It is simply named pprint
, which is no violation of any Python style guide.
精彩评论