Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why does not pprint work?

1,179 views
Skip to first unread message

fl

unread,
Jul 22, 2014, 5:42:02 PM7/22/14
to
Hi,

I read web tutorial at:

http://nedbatchelder.com/blog/201308/names_and_values_making_a_game_board.html

I enter the example lines of that website:


import pprint
board = [ [0]*8 ] * 8
pprint(board)


It echos error with Python 2.7:

Traceback (most recent call last):
File "C:\Python27\Lib\SITE-P~1\PYTHON~2\pywin\framework\scriptutils.py",
line 323, in RunScript
debugger.run(codeObject, __main__.__dict__, start_stepping=0)
File "C:\Python27\Lib\SITE-P~1\PYTHON~2\pywin\debugger\__init__.py",
line 60, in run
_GetCurrentDebugger().run(cmd, globals,locals, start_stepping)
File "C:\Python27\Lib\SITE-P~1\PYTHON~2\pywin\debugger\debugger.py",
line 655, in run
exec cmd in globals, locals
File "C:\cygwin64\home\Jeff\Python_lesson\ppn.py", line 1, in <module>
import pprint
TypeError: 'module' object is not callable

It has similar error with Python 3.4.1.


Why does pprint not work?


Thanks,

Chris Angelico

unread,
Jul 22, 2014, 5:49:53 PM7/22/14
to pytho...@python.org
On Wed, Jul 23, 2014 at 7:42 AM, fl <rxj...@gmail.com> wrote:
> I enter the example lines of that website:
>
>
> import pprint
> board = [ [0]*8 ] * 8
> pprint(board)

Flaw in the blog post: he didn't actually specify the import line.
What you actually want is this:

from pprint import pprint

Or use pprint.pprint(board), but you probably don't need anything else
from the module.

Ned, if you're reading this: Adding the import would make the post clearer. :)

ChrisA

emile

unread,
Jul 22, 2014, 5:51:07 PM7/22/14
to pytho...@python.org
On 07/22/2014 02:42 PM, fl wrote:
> Hi,
>
> I read web tutorial at:
>
> http://nedbatchelder.com/blog/201308/names_and_values_making_a_game_board.html
>
> I enter the example lines of that website:
>
>
> import pprint
> board = [ [0]*8 ] * 8
> pprint(board)
>
>

pprint is a module name -- you need to invoke the pprint function from
within the pprint module:

pprint.pprint(board)

or alternately,

from pprint import pprint

pprint(board)


or, as I sometime do

from pprint import pprint as pp

pp(board)



Emile


fl

unread,
Jul 22, 2014, 6:05:23 PM7/22/14
to
On Tuesday, July 22, 2014 5:51:07 PM UTC-4, emile wrote:
> On 07/22/2014 02:42 PM, fl wrote:
> pprint is a module name -- you need to invoke the pprint function from
> within the pprint module:
> pprint.pprint(board)

Thanks. I am curious about the two pprint. Is it the first pprint the name of the
module? The second pprint is the function name?

Then, how can I list all the function of pprint?

And, is there a way to list the variables I create in Python?

emile

unread,
Jul 22, 2014, 6:17:49 PM7/22/14
to pytho...@python.org
On 07/22/2014 03:05 PM, fl wrote:
> On Tuesday, July 22, 2014 5:51:07 PM UTC-4, emile wrote:
>> On 07/22/2014 02:42 PM, fl wrote:
>> pprint is a module name -- you need to invoke the pprint function from
>> within the pprint module:
>> pprint.pprint(board)
>
> Thanks. I am curious about the two pprint. Is it the first pprint the name of the
> module? The second pprint is the function name?

Yes.


> Then, how can I list all the function of pprint?

use the dir builtin:

>>> dir (pprint)
['PrettyPrinter', '_StringIO', '__all__', '__builtins__', '__doc__',
'__file__', '__name__', '_commajoin', '_id', '_len', '_perfcheck',
'_recursion', '_safe_repr', '_sys', '_type', 'isreadable',
'isrecursive', 'pformat', 'pprint', 'saferepr']


>
> And, is there a way to list the variables I create in Python?

also dir:

>>> dir()
['__builtins__', '__doc__', '__name__', 'board', 'mylist', 'pprint',
'reassign']


Emile


Chris Angelico

unread,
Jul 22, 2014, 6:19:45 PM7/22/14
to pytho...@python.org
On Wed, Jul 23, 2014 at 8:05 AM, fl <rxj...@gmail.com> wrote:
> On Tuesday, July 22, 2014 5:51:07 PM UTC-4, emile wrote:
>> On 07/22/2014 02:42 PM, fl wrote:
>> pprint is a module name -- you need to invoke the pprint function from
>> within the pprint module:
>> pprint.pprint(board)
>
> Thanks. I am curious about the two pprint. Is it the first pprint the name of the
> module? The second pprint is the function name?

Correct. There's a module pprint which provides a function
pprint.pprint. It's like you can do this:
>>> import math
>>> math.sin(3.14/2)
0.9999996829318346

Or this:
>>> from math import sin
>>> sin(3.14/2)
0.9999996829318346

It's just that in this case, "math" and "sin" are both "pprint".

> Then, how can I list all the function of pprint?

>>> import pprint
>>> help(pprint)

> And, is there a way to list the variables I create in Python?

Kinda. Try this:

>>> dir()

There'll be some in there that you didn't make, but that's a start.
You could also try vars() or globals(), which will give you their
values as well.

ChrisA

Chris Kaynor

unread,
Jul 22, 2014, 6:22:41 PM7/22/14
to emile, pytho...@python.org
On Tue, Jul 22, 2014 at 3:17 PM, emile <em...@fenx.com> wrote:
Then, how can I list all the function of pprint?

use the dir builtin:

>>> dir (pprint)
['PrettyPrinter', '_StringIO', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '_commajoin', '_id', '_len', '_perfcheck', '_recursion', '_safe_repr', '_sys', '_type', 'isreadable', 'isrecursive', 'pformat', 'pprint', 'saferepr']

Another useful feature is the "help" function (can be used like "help(pprint)" or just "help()" for interactive usage), which will also provide the documentation for each item in addition to just the names.

Chris

Ned Batchelder

unread,
Jul 22, 2014, 10:26:55 PM7/22/14
to pytho...@python.org
On 7/22/14 5:49 PM, Chris Angelico wrote:
> On Wed, Jul 23, 2014 at 7:42 AM, fl <rxj...@gmail.com> wrote:
>> I enter the example lines of that website:
>>
>>
>> import pprint
>> board = [ [0]*8 ] * 8
>> pprint(board)
>
> Flaw in the blog post: he didn't actually specify the import line.
> What you actually want is this:
>
> from pprint import pprint
>
> Or use pprint.pprint(board), but you probably don't need anything else
> from the module.
>
> Ned, if you're reading this: Adding the import would make the post clearer. :)

Done.

>
> ChrisA
>


--
Ned Batchelder, http://nedbatchelder.com

Chris Angelico

unread,
Jul 23, 2014, 1:05:33 AM7/23/14
to pytho...@python.org
On Wed, Jul 23, 2014 at 12:26 PM, Ned Batchelder <n...@nedbatchelder.com> wrote:
>> Ned, if you're reading this: Adding the import would make the post
>> clearer. :)
>
>
> Done.

Thanks Ned!

ChrisA

robko...@gmail.com

unread,
Aug 3, 2014, 5:34:04 PM8/3/14
to
With the way you have imported, you trying to use the module pprint instead of the function pprint.pprint.

You need to use pprint.pprint or you need to import as:

from pprint import pprint

if you want to use the shorter form.

Mark Lawrence

unread,
Aug 3, 2014, 6:49:53 PM8/3/14
to pytho...@python.org
On 03/08/2014 22:34, robko...@gmail.com wrote:

[snipped to bits]

Please don't top post, further would you read and action this
https://wiki.python.org/moin/GoogleGroupsPython to prevent us seeing
double line spacing and single line paragraphs, thanks.

--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

0 new messages