所以我在尝试学习leap-motion SDK并在4-5年没有接触过python之后重新学习它,我在python2.7中遇到了一个生成器问题
基本上,我有一个单词列表,每次跳跃动作拿起一个新的“圆圈”手势时,我想打印列表中的下一个单词。我看到的是每次on_frame
回调函数只打印列表中的第一个单词。我相信发生的是python运行时在事件之间忘记了生成器的状态。有没有办法在手势事件之间保持生成器状态?在
if not frame.hands.is_empty:
for gesture in frame.gestures():
if gesture.type == Leap.Gesture.TYPE_CIRCLE:
circle = CircleGesture(gesture)
# Determine clock direction using the angle between the pointable and the circle normal
action = None
if circle.pointable.direction.angle_to(circle.normal) <= Leap.PI/4:
action = GestureActions.clockwiseCircleGesture()
else:
action = GestureActions.counterClockwiseCircleGesture()
print action.next()
def clockwiseCircleGesture():
words = ["You", "spin", "me", "right", "round", "baby", "right", "round", "like", "a", "record", "baby", "Right", "round", "round", "round", "You", "spin", "me", "right", "round", "baby", "Right", "round", "like", "a",
"record", "baby", "Right", "round", "round", "round"]
for word in words:
yield word
任何对这方面的洞察都会很好。谢谢
对python不熟悉,但当人们学习生成器/迭代器/枚举器/任何东西时,这是一个常见的问题。每次遍历循环时都会重新创建迭代器并丢失状态。在
而是最多创建一次
我怀疑每次触发事件时,
action
变量都会被重置。在在事件处理函数之外初始化生成器。看起来您可能想要两个,}。在
clockwise_action
和{相关问题 更多 >
编程相关推荐