Problems with debugging Lists

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri Dec 11 21:52:26 EST 2009


En Fri, 11 Dec 2009 19:11:38 -0300, Sancar Saran <sancar.saran at evodot.com>  
escribió:

> In php we had print_r function to see entire array structure. After some
> search I found some equal module named pprint.
>
> And some how this module wont work with mod_wsgi it was something about
> mod_wsgi portability standards.
>
> After some research there where some thing about putting some variables  
> in
> apache config to disable this.
>
> And now I can see some dictionary structure in my apache log and I got  
> some
> errors like
> r += pprint.pprint(self.data)
> TypeError: cannot concatenate 'str' and 'NoneType' objects

The pprint function in the pprint module (that is, pprint.pprint) *prints*  
its argument, and returns nothing -- or, better said, it returns None  
(same as print_r in PHP, without the return parameter set to true)

> So is there any way to get dictionary structure in string format ?

You don't need anything special for that. There are two built-in functions  
that convert any object to string: str and repr. str(x) provides a simple  
representation of x (whatever it is), and repr(x) provides a more  
technical view; when possible, eval(repr(x)) should return x.
For debugging purposes, repr() is your friend.
pprint.pformat is like the built-in repr(), but provides a better  
formatted representation, with indenting, a maximum width, etc.

> Another question is. When I import a module from top is it available for  
> later
> imported modules

Each module contains its own, separate namespace. If you `import foo` in  
some module, the name `foo` becomes available to be used in that module --  
if you want to use `foo` in another module, you have to `import foo` in  
that other module too.

Don't worry; after the very first import (which involves locating the  
module, loading and compiling it if necesary, and writing the .pyc file)  
any subsequent imports of the same module just return a new reference to  
the existing, in-memory module object.

-- 
Gabriel Genellina




More information about the Python-list mailing list