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
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com