py3k printing generators -- not!

Carl Banks pavlovevidence at gmail.com
Sat Jun 6 08:58:53 EDT 2009


On Jun 6, 5:28 am, samwyse <samw... at gmail.com> wrote:
> The one thing that's killing me in Python 3000 is that every time I
> try to print something, it seems like I get <generator object
> <genexpr> at 0x01BAF508>.  Googling only found one reference, a
> posting elsewhere by one Carl Johnson (aka carlj7,http://www.artima.com/forums/flat.jsp?forum=106&thread=211200#275387),
> which apparently was never answered.  Is anyone else finding this
> bothersome, or is it entirely due to my FP background?
>
> Always saying "print(','.join(x))" gets tiresome in a hurry.  

What about print(list(x))

> I've
> thought about defining my own function "prnt" that wraps print and
> fixes generators, but that requires me to get their type, which
> despite the claims of "help(type(x for x in range(0)))" cannot be
> found in builtins.

Interestingly, the fact that it wasn't in builtins didn't stop you
from being able to pass the type to the help() function.  I wonder if
you can use the same trick to obtain the type for use in your prnt()
function.

(Failing that, you could use "from types import GeneratorType".)

> How are other solving this?

In my experience, if you want a custom print function that prints
things the way you want, you have to write it yourself.  People's
expectations are too different.


Carl Banks



More information about the Python-list mailing list