This is best illustrated by an example (all examples assume that it is astimported, note that I am using Python 2.7.1):
# Outputs: Slice(lower=Num(n=1), upper=Num(n=10), step=None)
ast.dump(ast.parse("l[1:10]").body[0].value.slice)
# Outputs: Slice(lower=Num(n=1), upper=Num(n=10), step=Name(id='None', ctx=Load()))
ast.dump(ast.parse("l[1:10:]").body[0].value.slice)
# Outputs: Slice(lower=Num(n=1), upper=None, step=None)
ast.dump(ast.parse("l[1:]").body[0].value.slice)
# Outputs: Slice(lower=None, upper=None, step=None)
ast.dump(ast.parse("l[:]").body[0].value.slice)
So, as we can see, it l[1:10]leads to an AST node, whose slice has two children - lowerand upperset both numeric literals and the empty third child step. But [1:10:], which we will consider identical, sets its slice stepchild as a literal None( Name(id='None', ctx=Load())).
Good, I thought. Perhaps, Python treats l[1:10:]and l[1:10]how completely different kinds of expressions. The reference to the Python expression ( link ) has undoubtedly shown this; l[1:10]- simple slicing, but l[1:10:]- extended slicing (with only one slice element).
But even in the context of an extended slice, the step argument is handled specially. If we try to ignore the upper or lower border in an extended section with one slice element, we just end up with empty child elements:
# Outputs: Slice(lower=Num(n=1), upper=None, step=Name(id='None', ctx=Load()))
ast.dump(ast.parse("l[1::]").body[0].value.slice)
# Outputs: Slice(lower=None, upper=Num(n=10), step=Name(id='None', ctx=Load()))
ast.dump(ast.parse("l[:10:]").body[0].value.slice)
In addition, upon further examination, AST does not even treat these sections as extended bevels. Here, the extended slices actually look:
# Outputs: ExtSlice(dims=[Slice(lower=None, upper=None, step=Name(id='None', ctx=Load())), Slice(lower=None, upper=None, step=Name(id='None', ctx=Load()))])
ast.dump(ast.parse("l[::, ::]").body[0].value.slice)
, : AST step - , , Slice AST node ( , Slice classes- ShortSlice LongSlice - , ), Slice node . None , , ; None Slice ( ).
- ?