[issue35200] Make the half-open range behaviour easier to teach

2018-11-21 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

As a teacher, I think the proposal makes us worst off.  It is far easier and 
more useful at the interactive prompt to use list() rather than print() to show 
ranges:

>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(2, 10))
[2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(2, 10, 3))
[2, 5, 8]

If you do the same thing with print(), it takes an additional character 
("print" vs "list"), it creates a new source of confusion (str vs repr), and it 
doesn't generalize to other iterators like enumerate(), reversed(), and 
generators.

Also, the various ideas listed for a possible new __str__ are all awkward or 
mysterious for some inputs (empty ranges, short ranges, etc).

FWIW, I teach this topic every week.  Presenting with list(range(...)) is less 
convenient than with the Python 2.7 version, but it works out just fine in 
practice and nicely sets the stage for covering set(iterable), tuple(iterable), 
dict.fromkeys(iterable), etc.

I'm opposed the this proposal because I think it will create more teaching 
difficulties than it solves.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35200] Make the half-open range behaviour easier to teach

2018-11-19 Thread Steven D'Aprano


Steven D'Aprano  added the comment:

Raymond:
> I'm in agreement with the comments that the proposed __str__ revision is 
> confusing.

In what way is it "confusing"?

I'm especially perplexed that Julien apparently thinks it is confusing when 
emitted by str(), but educational and useful when emitted by repr(). This makes 
no sense to me.

I think Julien's idea is a good one, just not for repr, and I don't think it is 
confusing at all.


Raymond:
> most other iterators can't show a preview of the output without actually 
> consuming some of their inputs

`range` is not some arbitrary iterator, in fact it isn't an iterator at all:

py> r = range(10)
py> iter(r) is r
False


It is a sequence, like list and tuple, and like list and tuple it is perfectly 
capable of showing its content (in full or part) on demand. Other important 
built-in sequence types like strings, lists and tuples aren't hamstrung with 
the restriction not to do anything iterators can't do, there's no good reason 
for range objects to be given that restriction.


Julien:
> I'm closing this.

Not so hasty, please. Some of us think this is a worthwhile enhancement. You 
might have changed your mind, but the idea is bigger than you now :-)

I'm taking this discussion to Python-Ideas to see if there is community 
interest in this feature. If so, I'm going to reopen the issue.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35200] Make the half-open range behaviour easier to teach

2018-11-19 Thread Julien Palard


Julien Palard  added the comment:

Hi Raymond,

I agree, there exist other means of teaching half closed range, but I was more 
concerned by self-taught students, alone facing the current range repr than 
students well accompanied.

I also agree, let's not change the current repr (for backward compatibility) 
and let's not change the current str (it won't help anyway), so I'm closing 
this.

--
resolution:  -> rejected
stage: patch review -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35200] Make the half-open range behaviour easier to teach

2018-11-19 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

After more thought, I'm in agreement with the comments that the proposed 
__str__ revision is confusing.

After teaching another Python intro course last week, I'm now thinking that no 
change should be made.  There are other effective ways to teach half-open 
intervals (i.e. using slicing on strings is now my preferred way). 

Also students need to learn about using list() with iterators.  This core skill 
comes up with generators, enumerate, zip, filter, etc. So, we just need to 
teach the skill earlier in the course than we did with Python2.7.

I recommend that we just close this and resist the urge to create a new oddity 
that does't generalize well (i.e. most other iterators can't show a preview of 
the output without actually consuming some of their inputs).

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35200] Make the half-open range behaviour easier to teach

2018-11-19 Thread Steven D'Aprano

Steven D'Aprano  added the comment:

On Sun, Nov 18, 2018 at 10:27:11PM +, Julien Palard wrote:
> 
> Julien Palard  added the comment:
> 
> If I understand correctly, you'd like str(range(10)) to return " [1, 2, ..., 8, 9]>"?

Exactly the same as you suggested for repr(range(10)) to return, so yes.

> I'm really unconfortable doing this, for me __str__ is here to return 
> an “informal or nicely printable string representation of an object", 

I think that the output you suggested is an informal AND nicely 
printable string representation of the object. In what way do you think 
it fails?

It's an *informal* representation in the sense that it doesn't mimic the 
range constructor, you can't evaluate it, it isn't even legal Python 
syntax.

"Nicely printable" is a matter of taste, but I think its quite nice 
(just not suitable for use as the repr), and especially nice for the 
purpose of showing the kind of object we're dealing with, rather than 
just the values in it.

> not a convoluted "<{type(object)} object ...>" notation.

If this is too convoluted for str(), why is it suitable for beginners 
when it goes through repr() instead?

> I agree with you, the [0, 1, ..., 8, 9] notation is too confusing with 
> the repr of a list, that's why I proposed the "0, 1, ..., 8, 9" which 
> looks nice.

Except that it gives no clue that it is a range object, and fails for 
empty ranges.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35200] Make the half-open range behaviour easier to teach

2018-11-18 Thread Julien Palard

Julien Palard  added the comment:

If I understand correctly, you'd like str(range(10)) to return ""?

I'm really unconfortable doing this, for me __str__ is here to return an 
“informal or nicely printable string representation of an object", not a 
convoluted "<{type(object)} object ...>" notation.

I agree with you, the [0, 1, ..., 8, 9] notation is too confusing with the repr 
of a list, that's why I proposed the "0, 1, ..., 8, 9" which looks nice.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35200] Make the half-open range behaviour easier to teach

2018-11-18 Thread Steven D'Aprano


Steven D'Aprano  added the comment:

On Sun, Nov 18, 2018 at 09:43:02PM +, Julien Palard wrote:

> My first though went to giving something really simple like:
> 
> >>> print(range(10))
> 1, 2, ..., 8, 9

-1 

Surely that would be your *second* thought, since you already had a 
perfectly adequate first thought:

 

is explicit about what kind of object we have. Remember, there will be 
times where people don't know they have a range object, and are printing 
it to find out what they have.

Let's just move that from __repr__ to __str__.

> But for the empty range it would give an empty string. It may make 
> sense, but may also be surprising.
> 
> The other way would be to print [1, 2, ..., 8. 9], so the empty range gets [] 
> instead of nothing.

Certainly not. That looks like a list containing 1, 2, ellipsis, 8, 9, 
and will only increase confusion about the difference between lists and 
range objects.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35200] Make the half-open range behaviour easier to teach

2018-11-18 Thread Julien Palard


Julien Palard  added the comment:

My first though went to giving something really simple like:

>>> print(range(10))
1, 2, ..., 8, 9

But for the empty range it would give an empty string. It may make sense, but 
may also be surprising.

The other way would be to print [1, 2, ..., 8. 9], so the empty range gets [] 
instead of nothing.

I think I prefer the first way.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35200] Make the half-open range behaviour easier to teach

2018-11-18 Thread Julien Palard


Julien Palard  added the comment:

In one hand I'm OK to enhance the __str__ of range, so I'll change my PR for 
this.

It will not fix the issue, but let's not break backward compatibility.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35200] Make the half-open range behaviour easier to teach

2018-11-18 Thread Nick Coghlan


Nick Coghlan  added the comment:

(Retitled the issue to better reflect the underlying feature request)

As Steven describes, there are enough problems with changing range.__repr__ 
that if that's the proposal, then the only possible answer is "No", and closing 
the issue.

However, changing range.__str__ (and hence print, f-strings, logging, and more) 
offers many of the same benefits, without most of the downsides (repr will 
still roundtrip through eval, doctests won't break, etc).

The only potential benefit that gets lost is the fact that entering "range(10)" 
at the REPL will still print "range(0, 10)", such that you need to do 
"print(range(10))" to get the version that shows the endpoint values. For 
longer ranges, "print(range(100))" will still end up being a lot more user 
friendly than "print(list(100))".

--
title: Range repr could be better -> Make the half-open range behaviour easier 
to teach

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com