At various points, there have been comments that xrange objects should not print as lists but as xrange objects. Taking a look at the implementation, I noticed that if you call repr() (by name or by backtick syntax), you get "the right thing"; the list representation comes up when you print the object on a real file object. The tp_print slot of the xrange type produces the list syntax. There is no tp_str handler, so str(xrange(...)) is the same as repr(xrange(...)). I propose ripping out the tp_print handler completely. (And I've already tested my patch. ;) Comments? -Fred -- Fred L. Drake, Jr. <fdrake at beopen.com> BeOpen PythonLabs Team Member
On Thu, 3 Aug 2000, Fred L. Drake, Jr. wrote:
I propose ripping out the tp_print handler completely. (And I've already tested my patch. ;) Comments?
+1. Like I always say: less code, less bugs. -- Moshe Zadka <moshez@math.huji.ac.il> There is no IGLU cabal. http://advogato.org/person/moshez
On Thu, Aug 03, 2000 at 09:06:51AM -0400, Fred L. Drake, Jr. wrote:
There is no tp_str handler, so str(xrange(...)) is the same as repr(xrange(...)). I propose ripping out the tp_print handler completely. (And I've already tested my patch. ;) Comments?
+0... I would say 'swap str and repr', because str(xrange) does what repr(xrange) should do, and the other way 'round:
x = xrange(1000) repr(x) (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, ... ... ... ... 998, 999)
str(x) '(xrange(0, 1000, 1) * 1)'
But I don't really care either way. -- Thomas Wouters <thomas@xs4all.net> Hi! I'm a .signature virus! copy me into your .signature file to help me spread!
Thomas Wouters writes:
x = xrange(1000) repr(x) (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, ... ... ... ... 998, 999)
str(x) '(xrange(0, 1000, 1) * 1)'
What version is this with? 1.5.2 gives me: Python 1.5.2 (#1, May 9 2000, 15:05:56) [GCC 2.95.3 19991030 (prerelease)] on linux-i386 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
x = xrange(2) str(x) '(xrange(0, 2, 1) * 1)' repr(x) '(xrange(0, 2, 1) * 1)' x (0, 1)
The 1.6b1 that's getting itself ready says this: Python 1.6b1 (#19, Aug 2 2000, 01:11:29) [GCC 2.95.3 19991030 (prerelease)] on linux2 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam Copyright 1995-2000 Corporation for National Research Initiatives (CNRI) Module readline not available.
x = xrange(2) str(x) '(xrange(0, 2, 1) * 1)' repr(x) '(xrange(0, 2, 1) * 1)' x (0, 1)
What I'm proposing is: Python 2.0b1 (#116, Aug 2 2000, 15:35:35) [GCC 2.95.3 19991030 (prerelease)] on linux2 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam Copyright 1995-2000 Corporation for National Research Initiatives (CNRI)
x = xrange(2) str(x) 'xrange(0, 2, 1)' repr(x) 'xrange(0, 2, 1)' x xrange(0, 2, 1)
(Where the outer (... * n) is added only when n != 1, 'cause I think that's just ugly.) -Fred -- Fred L. Drake, Jr. <fdrake at beopen.com> BeOpen PythonLabs Team Member
On Thu, Aug 03, 2000 at 11:14:57AM -0400, Fred L. Drake, Jr. wrote:
Thomas Wouters writes:
x = xrange(1000) repr(x) (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, ... ... ... ... 998, 999)
str(x) '(xrange(0, 1000, 1) * 1)'
What version is this with? 1.5.2 gives me:
Python 1.5.2 (#1, May 9 2000, 15:05:56) [GCC 2.95.3 19991030 (prerelease)] on linux-i386 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
x = xrange(2) str(x) '(xrange(0, 2, 1) * 1)' repr(x) '(xrange(0, 2, 1) * 1)' x (0, 1)
Sorry, my bad. I just did 'x', and assumed it called repr(). I guess my newbiehood shows in that I thought 'print x' always called 'str(x)'. Like I replied to Tim this morning, after he caught me in the same kind of ebmarrasing thinko: Sigh, that's what I get for getting up when my GF had to and being at the office at 8am. Don't mind my postings today, they're likely 99% brainfart. Seeing as how 'print "range: %s"%x' did already use the 'str' and 'repr' output, I see no reason not to make 'print x' do the same. So +1.
x xrange(0, 2, 1)
(Where the outer (... * n) is added only when n != 1, 'cause I think that's just ugly.)
Why not remove the first and last argument, if they are respectively 0 and 1?
xrange(100) xrange(100) xrange(10,100) xrange(10, 100)
-- Thomas Wouters <thomas@xs4all.net> Hi! I'm a .signature virus! copy me into your .signature file to help me spread!
Thomas Wouters writes:
Sorry, my bad. I just did 'x', and assumed it called repr(). I guess my newbiehood shows in that I thought 'print x' always called 'str(x)'. Like I
That's the evil beauty of tp_print -- nobody really expects it because most types don't implement it (and don't need to); I seem to recall Guido saying it was a performance issue for certain types, but don't recall the specifics.
Why not remove the first and last argument, if they are respectively 0 and 1?
I agree! In fact, always removing the last argument if it == 1 is a good idea as well. Here's the output from the current patch:
xrange(2) xrange(2) xrange(2, 4) xrange(2, 4) x = xrange(10, 4, -1) x xrange(10, 4, -1) x.tolist() [10, 9, 8, 7, 6, 5] x*3 (xrange(10, 4, -1) * 3)
-Fred -- Fred L. Drake, Jr. <fdrake at beopen.com> BeOpen PythonLabs Team Member
participants (3)
-
Fred L. Drake, Jr. -
Moshe Zadka -
Thomas Wouters