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

Nick Coghlan ncoghlan at gmail.com
Thu Aug 19 11:47:32 CEST 2010


On Thu, Aug 19, 2010 at 6:36 PM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> 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?

Probably so it looks different from the AST for x[:]

>>> ast.dump(ast.parse("x[:]", mode='eval'))
"Expression(body=Subscript(value=Name(id='x', ctx=Load()),
slice=Slice(lower=None, upper=None, step=None), ctx=Load()))"
>>> ast.dump(ast.parse("x[::]", mode='eval'))
"Expression(body=Subscript(value=Name(id='x', ctx=Load()),
slice=Slice(lower=None, upper=None, step=Name(id='None', ctx=Load())),
ctx=Load()))"

Or else it's just an accident of implementation, since the AST doesn't
actually *need* to distinguish those two cases.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list