1

我目前正在为 Python3 中的 Ableton live 11 开发步进音序器。这是我手动从 Python2 转换为 3 的代码。我正在定义一个可能为空也可能不是空的列表,因此我插入了一个检查以使用 if 语句查看列表是否为空,但它不是发现列表为空。这是我当前问题的一些代码:

      notes = self._time_step(time).filter_notes(self._clip_notes)
      if notes:
          most_significant_velocity =  max(notes, key=lambda n: n[3])
          do something
      else:
          do something different

我收到以下错误:

2021-11-05T12:18:12.100094: info: RemoteScriptError: most_significant_velocity = max(notes, key=lambda n: n[3]) 
2021-11-05T12:18:12.100138: info: RemoteScriptError: 

2021-11-05T12:18:12.100184: info: RemoteScriptError: ValueError
2021-11-05T12:18:12.100227: info: RemoteScriptError: : 
2021-11-05T12:18:12.100271: info: RemoteScriptError: max() arg is an empty sequence

在这种情况下,Notes 是一个空列表,因此“做其他事情”应该是被调用的内容。相反,if 语句没有捕捉到注释是空的。有关如何调试或解决此问题的任何想法?

4

2 回答 2

0

我同意你的看法——这对我来说似乎很奇怪。我会假设即使 notes 变量中没有真正的注释,它也持有某种类型的对象(其中没有任何注释),因此不会默认为 else。

我相信你知道这一点,但是当你写if variable_name:它时,它总是会触发,除非值variable_name = None

作为一个大 Ableton 人,我很想知道你在做什么,介意分享一个 repo 吗?:)

于 2021-11-16T08:40:45.593 回答
0

als文件中的notes xml树可以为空,如果没有notes则为空。

所以你可以包装一个 tuple(list) 来强制一个空列表。

note = (self._time_step(time).filter_notes(self._clip_notes),)
for n in notes:
    doSomething

或者

def parseNotes(self, notes=()):
    if type(notes) in (list, tuple):
        doSomething
于 2021-12-17T19:58:57.097 回答