Skip to content Skip to sidebar Skip to footer

I Don't Understand The Width Field In Pprint In Python

I don't understand this concept clearly. Could somebody give me some examples to demonstrate the concept for width in pprint in python?

Solution 1:

Basically it tries to limit your output to a specific width.

Here's an example:

importpprintstuff= ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
pp = pprint.PrettyPrinter(width=80)
pp.pprint(stuff)

result is:

['spam', 'eggs', 'lumberjack', 'knights', 'ni']

but if you do the same thing but change the width(say to 10):

importpprintstuff= ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
pp = pprint.PrettyPrinter(width=10)
pp.pprint(stuff)

you get:

['spam',
 'eggs',
 'lumberjack',
 'knights',
 'ni']

This is a modified example from the python docs ( http://docs.python.org/library/pprint.html ). For things like this, I find it easier to just open the python interrupter and play around and see how the commands react to what you enter.

Post a Comment for "I Don't Understand The Width Field In Pprint In Python"