[Python-Dev] Oddity in AST for 3-argument slices

Greg Ewing greg.ewing at canterbury.ac.nz
Thu Aug 19 10:36:54 CEST 2010


I've discovered a slightly surprising thing about the way
AST objects for slices are constructed. According to
Python.asdl, all three parts of a slice are optional:

    slice = Slice(expr? lower, expr? upper, expr? step)

But that's not quite the way the parser sees things:

Python 3.1.2 (r312:79147, Aug 19 2010, 20:26:20)
[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
 >>> import ast
 >>> t = ast.parse("x[::]", mode='eval')
 >>> ast.dump(t)
"Expression(body=Subscript(value=Name(id='x', ctx=Load()), 
slice=Slice(lower=None, upper=None, step=Name(id='None', ctx=Load())), ctx=Load()))"

In other words,

    x[::]

is being parsed as though it had been written

    x[::None]

Is there a good reason for an omitted third slice
argument being treated differently from the others?

-- 
Greg


More information about the Python-Dev mailing list