Python (1..n) 语法?

发布于 2024-09-15 09:35:13 字数 236 浏览 3 评论 0原文

我在此 Sage wiki 页面 上的代码中看到以下代码:

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

这是 (1. .n) Sage 独有的语法还是 Python 中的语法?另外,它有什么作用?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眸中客 2024-09-22 09:35:13

这是 Sage 特有的。您可以使用 preparse 来查看它是如何脱糖的:

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

请参阅此处获取ellipsis_iter的文档,此处 有关预解析器的信息。

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.

掩于岁月 2024-09-22 09:35:13

有一个 Python PEP 想要将此表示法添加到 Python 中,但被拒绝了。 Robert Bradshaw 决定无论如何都要实现它,但是对于 Sage 准备器来说。他实现了以下内容:

  • (a..b) -- 像 xrange,所以是一个迭代器

  • [a..b] - - 列表,包括端点

  • [a,b,..,c] -- 算术级数

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

断爱 2024-09-22 09:35:13

这不是 Python 语法。我猜它会创建一个从 1 到 12 的范围。

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

风月客 2024-09-22 09:35:13

Python 中不存在 (1..n) 语法。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文