py3k printing generators -- not!

Terry Reedy tjreedy at
Sat Jun 6 18:58:09 EDT 2009

samwyse wrote:
> The one thing that's killing me in Python 3000

py3.0 or py3.1, but the 'problem' you complain about has nothing to do 
with those versions in particular.

> is that every time I try to print something, it seems like I get <generator object
> <genexpr> at 0x01BAF508>.

Nor does it have anything is particular to do with generator objects. 
Str(function/class/module/and-many-others) prints similarly. Since 
forever, str(ob) == ob.__str__() == type(ob).__str__(ob).  And what you 
see above is the default template filled in for a particular object.

Built-in concrete collections over-ride the default and return a string 
with their contents because they can do that without harm other than 
possibly producing a very long string.  When the collections are large, 
one might want a custom function that instead formats a string with a 
limited number of examples and the total count.

A 3.0 range object, a virtual collection, could do that too, but since 
there is a regular pattern, it prints a condensed representation. 
Aren't you glad, actually, that range(1000000000) prints as "range(0, 
1000000000)" instead of listing the billion numbers it produces when 
iterated, as you seem to be asking for?

>  Googling only found one reference, a
> posting elsewhere by one Carl Johnson (aka carlj7,

A bogus complaint that the constructor for highly specialized class str 
acts differently from those of the general collection classes set, 
tuple, and list.  Str is actually not a collection class in the way that 
set, tuple, list, dict, and many others are.

Terry Jan Reedy

More information about the Python-list mailing list