classification
Title: strange string representation of xrange in print
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 2.6, Python 2.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, mintaka, rhettinger
Priority: normal Keywords:

Created on 2009-08-22 19:12 by mintaka, last changed 2009-08-22 22:21 by rhettinger. This issue is now closed.

Messages (4)
msg91871 - (view) Author: Mintaka (mintaka) Date: 2009-08-22 19:12
String representation of xrange return keyword with value.

foo = xrange(5)
print foo
>>> xrange(5)
foo.__str__() 
>>> xrange(5)

I think, that expected result should be somethink like this:
>>> <xrange object at 0x00AFB970>
msg91872 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2009-08-22 19:47
For types where it's possible, the str or repr returns a string that can
be passed to eval:

>>> for i in eval(str(xrange(5))):
...   print i
... 
0
1
2
3
4

xrange (and list, and tuple, and others) fall into this category.
msg91875 - (view) Author: Mintaka (mintaka) Date: 2009-08-22 20:35
Thanks for clarification. I compared it with iter([0,1,2,3,4]).__str__()
which behaviour seems to me closer then list or tuple.
msg91878 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2009-08-22 22:21
I concur with Eric.  The eval(repr(obj)) should be made to work when
possible.  We do this with itertools.count() and other places where it
makes sense.
History
Date User Action Args
2009-08-22 22:21:49rhettingersetnosy: + rhettinger
messages: + msg91878
2009-08-22 20:35:04mintakasetmessages: + msg91875
2009-08-22 19:48:00eric.smithsetstatus: open -> closed

nosy: + eric.smith
messages: + msg91872

resolution: not a bug
stage: resolved
2009-08-22 19:14:15mintakasettype: behavior
2009-08-22 19:12:19mintakacreate