Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #39894

Re: Suggested feature: slice syntax within tuples (or even more generally)?

Path csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <andrew3@r3dsolutions.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '*not*': 0.07; 'advocate': 0.07; 'indices': 0.07; 'received:64.202.165': 0.07; 'python': 0.09; 'any.': 0.09; 'compute': 0.09; 'counting': 0.09; 'expectation': 0.09; 'integers': 0.09; 'loop.': 0.09; 'semantics': 0.09; 'slices': 0.09; '24,': 0.16; 'inclusive.': 0.16; 'infinite;': 0.16; 'int(a)': 0.16; 'iterator': 0.16; 'list;': 0.16; 'loops': 0.16; 'mainstream': 0.16; 'range,': 0.16; 'range.': 0.16; 'ranges.': 0.16; 'subject:)?': 0.16; 'syntax,': 0.16; 'value;': 0.16; 'wrote:': 0.17; 'typing': 0.17; 'feb': 0.19; 'parameters': 0.20; 'all,': 0.21; 'converted': 0.22; 'explicit': 0.22; 'ones.': 0.22; 'this:': 0.23; "haven't": 0.23; "i've": 0.23; 'raise': 0.24; 'second': 0.24; 'pass': 0.25; 'header:User- Agent:1': 0.26; 'values': 0.26; 'am,': 0.27; 'andrew': 0.27; 'instead.': 0.27; 'actual': 0.28; 'run': 0.28; 'accidentally': 0.29; 'consistency': 0.29; 'idea,': 0.29; 'implicitly': 0.29; 'loop,': 0.29; 'necessary,': 0.29; 'prints': 0.29; 'no,': 0.29; 'skip:& 10': 0.29; 'error': 0.30; 'not.': 0.32; 'print': 0.32; 'true.': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'code:': 0.33; 'agree': 0.34; 'same.': 0.35; 'sequence': 0.35; 'pm,': 0.35; 'continue': 0.35; 'there': 0.35; 'next': 0.35; 'list.': 0.35; 'really': 0.36; 'but': 0.36; 'programmers': 0.36; 'should': 0.36; 'subject: (': 0.36; 'enough': 0.36; 'bad': 0.37; 'one,': 0.37; 'does': 0.37; 'two': 0.37; 'rather': 0.37; 'subject:: ': 0.38; 'fact': 0.38; 'mean': 0.38; 'object': 0.38; 'to:addr:python.org': 0.39; 'build': 0.39; 'application': 0.40; 'where': 0.40; 'think': 0.40; 'your': 0.60; 'easy': 0.60; 'range': 0.60; 'you.': 0.61; 'first': 0.61; 'between': 0.63; 'respect': 0.63; 'different': 0.63; 'world': 0.63; 'reasons,': 0.65; 'header :Reply-To:1': 0.68; 'ranges': 0.71; 'reply-to:no real name:2**0': 0.72; '2013': 0.84; 'idiot': 0.84; 'stop,': 0.84; 'true!': 0.84; 'device,': 0.91; 'besides,': 0.93; 'yes!': 0.93; 'hand,': 0.97
Date Mon, 25 Feb 2013 08:45:38 +0000
From Andrew Robinson <andrew3@r3dsolutions.com>
User-Agent Mozilla/5.0 (X11; Linux i686; rv:15.0) Gecko/20120909 Thunderbird/15.0.1
MIME-Version 1.0
To python-list@python.org
Subject Re: Suggested feature: slice syntax within tuples (or even more generally)?
Content-Type multipart/alternative; boundary="------------010000020807000408070204"
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
Reply-To andrew3@r3dsolutions.com
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2508.1361810871.2939.python-list@python.org> (permalink)
Lines 295
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1361810871 news.xs4all.nl 6985 [2001:888:2000:d::a6]:49792
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:39894

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

On 02/25/2013 10:28 AM, Ian Kelly wrote:
> On Sun, Feb 24, 2013 at 6:10 PM, Andrew Robinson
> <andrew3@xxx> wrote:
>> I've read through the whole of the subject, and the answer is no, 
>> although I
>> think allowing it in (::) is a *very* good idea, including as a 
>> replacement
>> for range or xrange.
>>
>> s=1:2:3
>> for i in s:
>> for i in (1:2:3) :
> Eww, no.  I can appreciate the appeal of this syntax, but the problem
> is that ranges and slices are only superficially similar.  For one,
> ranges require a stop value; slices do not.  What should Python do
> with this:
>
> for i in (:):
The same thing it would do with slices.

A slice is converted to an iterator at the time __getitem__ is called; 
it in fact has methods to compute the actual start and stop, based on 
the parameters given and the size of the object it is applied to.  
Slices are, therefore, *not* in fact infinite;


>
> Intuitively, it should result in an infinite loop starting at 0. But
> ranges require a stop value for a very good reason -- it should not be
> this easy to accidentally create an infinite for loop.
It wouldn't, but even if it did an *effective* infinite loop is already 
easy to create with xrange:
a = 10
...
a = 1.1e12
...
for i in xrange( int(a) ):

and, besides, the same is true with other constructions of loops....

while a:  # Damn easy, if a is accidentally true!

I can go on....  but it's rather pointless.  Build a better protective 
device, and the world will find a luckier idiot for you. There isn't 
enough concrete to stop terrorists -- and not enough typing to stop bad 
programmers and pass the good ones.

>    So I would
> advocate that this should raise an error instead.  If the user really
> wants an unlimited counting loop, let them continue to be explicit
> about it by using itertools.count.  On the other hand, this would mean
> that the semantics of (:) would be different depending on whether the
> slice is used as a slice or a range.
No, it would be different depending on whether or not it was applied to 
an iterable; which is already true.

>
> The next problem you run into is that the semantics of negative
> numbers are completely different between slices and ranges. Consider
> this code:
>
> s = (-5:6)
> for i in s:
>      print(i)
> for i in range(6)[s]:
>      print(i)
I don't find this difference to be necessary, nor objectionable.

It is less inconsistent, in my view, to allow that
([ 1,2,3,4,5 ])[-1:2]  produce [5,1,2] than an empty list;
and ([ 1,2,3,4,5])[2:-1] does produce an empty list.

I have been looking for actual programs that this would break for over 
two months now, and I haven't been finding any.  I am willing to run any 
mainstream application you can find on test-patched python!

>
> Intuitively, both loops should print the same thing.  After all, one
> is using the slice s as a range, and the other is using the very same
> slice s as a slice of a sequence where the indices and values are the
> same.
YES! I like the way you think about consistency and intuition.

>    This expectation fails, however.  The first loop prints the
> integers from -5 to 5 inclusive, and the second loop only prints the
> integers from 1 to 5 inclusive.
>
> For these reasons, I disagree that allowing slices to be implicitly
> converted to ranges or vice versa is a good idea.
I respect your opinion and agree to simply disagree.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Suggested feature: slice syntax within tuples (or even more generally)? Andrew Robinson <andrew3@r3dsolutions.com> - 2013-02-25 08:45 +0000

csiph-web