py3k printing generators -- not!

samwyse samwyse at gmail.com
Sat Jun 6 17:11:32 CEST 2009


On Jun 6, 7:58 am, Carl Banks <pavlovevide... at gmail.com> wrote:
> On Jun 6, 5:28 am, samwyse <samw... at gmail.com> wrote:

> > Always saying "print(','.join(x))" gets tiresome in a hurry.  
>
> What about print(list(x))

Yeah, I like that.  Or, to save some typing:
  prnt = lambda x: print(list(x))

> 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.

Of course, but it would mean evaluating an expression every time I
defined my function.  That just seems unhygenic

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

Ah!  I should've thought of that.

> 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.

Well, that's why I rferenced the other article,
http://www.artima.com/forums/flat.jsp?forum=106&thread=211200#275387),
to show that other people have the same expectations.  (In the
article, Carl Johnson correctly points out that the real problem is
that str() responds to generators differently than do the other
collection types.)



More information about the Python-list mailing list