Python (1..n) syntax?

那年仲夏 提交于 2020-02-03 04:53:15

问题


I see in the code on this Sage wiki page the following code:

@interact
def _(order=(1..12)):

Is this (1..n) syntax unique to Sage or is it something in Python? Also, what does it do?


回答1:


It's Sage-specific. You can use preparse to see how it is desugared to:

sage: preparse("(1..12)")
'(ellipsis_iter(Integer(1),Ellipsis,Integer(12)))'

See here for documentation of ellipsis_iter, here for information on the preparser.




回答2:


There was a Python PEP to add this notation to Python, but it was rejected. Robert Bradshaw decided to implement it anyways, but for the Sage preparser. He implemented the following:

  • (a..b) -- like xrange, so an iterator

  • [a..b] -- list, including endpoints

  • [a,b,..,c] -- arithmetic progression




回答3:


This is not Python syntax. I would guess that it creates a range from 1 to 12.




回答4:


(1..n) syntax does not exist in Python.



来源:https://stackoverflow.com/questions/3511699/python-1-n-syntax

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!