【问题标题】:asterisk notation doesn't work in processing.py?星号符号在 processing.py 中不起作用?
【发布时间】:2020-06-04 03:16:26
【问题描述】:

我在常规 python 中使用星号表示法:

>>> x=(10,11)
>>> y=(12,13)
>>> z=99
>>> print(*x)
10 11
>>> print(*x, *y, z)
10 11 12 13 99

但是当我尝试在处理的 python 模式下做类似的事情时,它本质上给了我一个语法错误:processing.app.SketchException: Maybe there's an unclosed paren or quote mark somewhere before this line?

p1= (20,20)
p2=(40,40)
c1 = (15,15)
c2 = (50,50)

print(p1)
print(*p1)
# bezier(**p1, **c1, **c2, **p2)

Processing.PY 不支持这个吗?

【问题讨论】:

  • Python 处理模式的语法有限。
  • @KlausD。我不同意您的评论,Python 模式具有 完整 Python 2 工作语法和 Python 标准库的非常完整的 Jython 实现。

标签: python processing


【解决方案1】:

很好用,我一直在用。

p1 = (20, 20)
p2 = (40, 40)

translate(*p1)  # this works

# you can't use "star" unpacking twice like this.
# line(*p1, *p2)  # this won't work!

请注意,在 Python 2 中 print 不是函数,因此我使用 from __future__ import print_function 使您的示例在下面的图像上工作。 print 函数可以处理任意数量的位置参数,但同样不能 * "star" 解包两次。

我注意到您的问题示例** 可用于 关键字参数和字典解包。它总体上也可以正常工作,但不适用于您提供的 bezier() 评论。错误消息可能误导了您。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-17
    • 2011-09-25
    • 1970-01-01
    • 1970-01-01
    • 2013-08-04
    • 2021-09-21
    相关资源
    最近更新 更多