2

The docs at http://docs.python.org/reference/expressions.html#slicings say (with some excerpts):

5.3.3. Slicings

A slicing selects a range of items in a sequence object (e.g., a string, tuple or list).

slicing          ::=  simple_slicing | extended_slicing
simple_slicing   ::=  primary "[" short_slice "]"
extended_slicing ::=  primary "[" slice_list "]"
slice_list       ::=  slice_item ("," slice_item)* [","]
slice_item       ::=  expression | proper_slice | ellipsis
proper_slice     ::=  short_slice | long_slice
short_slice      ::=  [lower_bound] ":" [upper_bound]
long_slice       ::=  short_slice ":" [stride]
lower_bound      ::=  expression
upper_bound      ::=  expression
stride           ::=  expression
ellipsis         ::=  "..."

The semantics for a simple slicing are as follows. The primary must evaluate to a sequence object...

The semantics for an extended slicing are as follows. The primary must evaluate to a mapping object, ...

The question:

To support [1,2,3][a:b:c] notation the language reference requires lists to be mapping objects (the stride is there only with "extended slicing", which is for mapping objects). So, is the language reference broken (may be they just forgot to update it upon introduction of What's New in Python2.3: Extended Slices ?)?

Also, slicing is obviously not only for sequence objects (see the first phrase above).

Or is it just me? ;)

P.S.

Interestingly, Python 3 docs at http://docs.python.org/release/3.1.3/reference/expressions.html#slicings say:

A slicing selects a range of items in a sequence object (e.g., a string, tuple or list)...

["unified" slicing definition, not differentiating between "exteneded" and "simple" here]

The semantics for a slicing are as follows. The primary must evaluate to a mapping object, ...

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
mlvljr
  • 4,066
  • 8
  • 44
  • 61
  • 2
    Is it just me, or can I not understand you at all? Can you please clarify? – Pwnna May 31 '11 at 18:22
  • @ultimatebuster Or both of us.. :) Lists are not mapping objects, if I get it right, but stride is used only with exteneded slicing, which, in turn, requires mapping objects. – mlvljr May 31 '11 at 18:26
  • I've actually never heard of the term "stride". From what I make out in 3 minutes, it's the steps? Like `list[start:stop:stride]`? – Pwnna May 31 '11 at 18:28
  • In other words, what are you trying to do? xD – Pwnna May 31 '11 at 18:29
  • @ultimatebuster I'm trying to get the docs right now :) And it seems to me, that while `stride` (step) syntax for lists (and possibly other sequences -- I've just heve not looked at the appropriate parts yet) is there for already a long time, the docs (at least the page I'm referring to) say, it's only for mapping types -- obviously incorrectly. – mlvljr May 31 '11 at 18:34
  • I think you might be reading into the documentation too much. The language is highly technical, and causes some understanding issues (Try reading law papers, good luck :P). I don't even really understand what they are talking about. I'm not sure about this, but mapping object maybe true as everything in python is an object, and potentially a mapping object (Though I'm not too sure on that front.) – Pwnna May 31 '11 at 18:40
  • @ultimatebuster one may look at http://docs.python.org/library/stdtypes.html#mapping-types-dict to clarify that, it still seems to me the language reference isn't in sync with its actual state. – mlvljr May 31 '11 at 19:50
  • 1
    @mlvljr, agree with you that the language ref is not strictly correct – yorua007 Sep 20 '13 at 07:59
  • @yorua007 honestly, now I think the python docs only pretend to be describing things clearly -- it's all very shaky – mlvljr Sep 20 '13 at 21:19

2 Answers2

2

The language about a mapping is broken; it should say 'sequence or mapping' in all cases to match the behaviour of the interpreter. Both protocols can accept slice objects, and the interpreter will do the conversion in all cases. However, for the built-in types, only the sequences actually support it:

 >>> a = {'a': 1, 'c': 2}
 >>> a['a':'b']
 Traceback (most recent call last):
   File "<pyshell#32>", line 1, in <module>
     a['a':'b']
 TypeError: unhashable type: 'slice'

Note that this is the dictionary complaining that a slice isn't a valid key, not the interpreter complaining that you can't do a slice on a mapping. Which makes sense - dictionaries have no implied ordering of their keys, and so it is unclear what a slice should mean.

lvc
  • 34,233
  • 10
  • 73
  • 98
1

What's New and the langref are, unfortunately, using two different definitions of "extended slicing". Both documents are correct for their respective definitions.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • Ok, but how the langref is correct on strides--as-parts-of--extened-slices--which-are-only-for-mapping-objects still? – mlvljr May 31 '11 at 18:27
  • It's not. The langref is talking about using tuples as slices, e.g. `foo['bar', 42]`. – Ignacio Vazquez-Abrams May 31 '11 at 18:28
  • So, `[][0:1:2]`, does not fit into the langref's definitions, right? – mlvljr May 31 '11 at 18:30
  • 1
    I misread. "Extended slicing" in the langref covers both tuples and stride. – Ignacio Vazquez-Abrams May 31 '11 at 18:40
  • Seems like I've got not enough rep to link relevant questions, could it be possible that you link these to here: http://stackoverflow.com/questions/2761003/problem-with-list-slice-syntax-in-python, http://stackoverflow.com/questions/752602/slicing-in-python-expressions-documentation ? – mlvljr May 31 '11 at 21:26