tags:

views:

67

answers:

3

I've come across the following code in a Python script

from 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?)

A: 

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.

Ignacio Vazquez-Abrams
A: 

Yes, the syntax is from module import functions, so the first pprint is the module name and the second the function name.

sepp2k
+3  A: 

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?

Eloff
eloff: +1 for the use case that explained why anyone would want to do that. ok, I see whats going on. Modules provide a kind of 'namespacing' for Python objects. Still feels a bit weird though - I guess, I'll get used to it.
morpheous