Seek support for new slice syntax PEP.

D

Dave Angel

Colin said:
<div class="moz-text-flowed" style="font-family: -moz-fixed">On
You don't say, but seem to imply that the slice components include
None.

That's how missing components are implemented at the language level:
class foo:
= def __getitem__(self, s):
= return s
=
x = foo()
x[::] slice(None, None, None)
x[1::2]
slice(1, None, 2)

The defaults of zero, sys.maxint and one apply to built-in types, but
nothing forces user-defined types to behave this way.

Or maybe I misunderstood your point.
No, it seems that the implementation is a little different from the doc.

You are right:
*** Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32
bit (Intel)] on win32. ***
a= range(10)
a[2:8:2] [2, 4, 6]
a[2::2] [2, 4, 6, 8]
a[2:None:2] [2, 4, 6, 8]
I had expected the last to be rejected, but it fits with the overall
philosophy.

Colin W

</div>
None is perfectly valid as a parameter to a slice. To quote the 2.6.4
docs, in section 6.6:

The slice of /s/ from /i/ to /j/ with step /k/ is defined as the
sequence of items with index x = i + n*k such that 0 <= n < (j-i)/k. In
other words, the indices are i, i+k, i+2*k, i+3*k and so on, stopping
when /j/ is reached (but never including /j/). If /i/ or /j/ is greater
than len(s), use len(s). If /i/ or /j/ are omitted or None, they become
“end” values (which end depends on the sign of /k/). Note, /k/ cannot be
zero. If /k/ is None, it is treated like 1.


DaveA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top