- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我试图理解一些 python 2.5 代码,我遇到了这种模式:
def __init__(self, matrix, top_buttons, side_buttons, config_button):
raise isinstance(matrix, ButtonMatrixElement) or AssertionError
raise matrix.width() == 8 and matrix.height() == 8 or AssertionError
raise isinstance(top_buttons, tuple) or AssertionError
raise len(top_buttons) == 8 or AssertionError
raise isinstance(side_buttons, tuple) or AssertionError
raise len(side_buttons) == 8 or AssertionError
raise isinstance(config_button, ButtonElement) or AssertionError
我尝试在 shell 中使用一些简单的条件语句对此进行测试,如下所示:
>>> str = 'hello'
>>> raise len(str) == 5 or AssertionError
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
raise len(str) == 5 or AssertionError
TypeError: exceptions must be classes, instances, or strings (deprecated), not bool
所以从这个测试来看,至少我试过的方式,你不能引发一个 bool 语句。那么引发条件表达式是什么意思,为什么它在 __init__
函数中起作用但在我的测试代码中不起作用?
最佳答案
代码是胡说八道,是对看起来像 assert
statement 的拙劣尝试正如您所发现的,那失败了。
他们应该写的是:
assert isinstance(matrix, ButtonMatrixElement)
等等。
您似乎找到了 decompiled Ableton Live scripts ,但反编译脚本生成了错误的 Python 代码。 assert
的字节码如下所示(Python 2.5 字节码):
>>> import dis
>>> dis.dis(compile('''assert isinstance(matrix, ButtonMatrixElement)''', '<stdin>', 'exec'))
1 0 LOAD_NAME 0 (isinstance)
3 LOAD_NAME 1 (matrix)
6 LOAD_NAME 2 (ButtonMatrixElement)
9 CALL_FUNCTION 2
12 JUMP_IF_TRUE 7 (to 22)
15 POP_TOP
16 LOAD_GLOBAL 3 (AssertionError)
19 RAISE_VARARGS 1
>> 22 POP_TOP
23 LOAD_CONST 0 (None)
26 RETURN_VALUE
并且看起来好像使用任何自动过程来反编译字节码将其翻译成您看到的代码,而不是将其识别为断言
。
但是请注意,如果 isinstance()
调用返回 True
,则跳转指令(索引 12,JUMP_IF_TRUE
)将跳转过去 RAISE_VARARGS
指令,而重构代码则没有。将此与实际的 raise ... or ...
语句进行比较,您会注意到跳转不会超过 raise
:
>>> dis.dis(compile('raise foo or bar', '<stdin>', 'exec'))
1 0 LOAD_NAME 0 (foo)
3 JUMP_IF_TRUE 4 (to 10)
6 POP_TOP
7 LOAD_NAME 1 (bar)
>> 10 RAISE_VARARGS 1
13 LOAD_CONST 0 (None)
16 RETURN_VALUE
大概是代码生成器处理这个问题不够复杂;如果您假设只有 或
生成 JUMP_IF_TRUE
并且没有正确处理偏移量,您可以看到错误是如何产生的。
关于python - "raise"后跟条件语句(python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26336545/
我想拦截以\*#\*开头的字符串 后跟一个 0 到 7 之间的数字 并以:##结尾 所以像\*#\*0## 但是我找不到这个的正则表达式 最佳答案 假设你想允许只有一个 # 之前和两个之后,我会这样做
在阅读 C# 代码时,我发现了一个相当奇怪的片段: if( whatever is IDisposable) { (whatever as IDisposable).Dispose(); } 我宁
我在看别人的框架代码,我发现其中一个函数的参数接受了一个输入,输入后面跟着(.),当我检查输入的类型时,我发现它是一个结构不是枚举,这让我很困惑 这是函数的屏幕截图,输入看起来像一个枚举案例: 参数的
我对地址运算符后跟解引用运算符的行为有疑问。 让我们看一下表达式 &*p,其中 p 的类型为 int *。 C11 标准(第 6.5.3.2 节)说: The unary & operator yie
在我的团队中,我经常看到队友在写作 list.filter(_.isInstanceOf[T]).map(_.asInstanceOf[T]) 但这对我来说似乎有点多余。 如果我们知道过滤列表中的所有
请帮我解决这个问题。我已经尝试过删除和重新创建数据文件、刷新表、重新启动数据库和整个服务器。 InnoDB 处于 Force Recovery = 4 >mysql -u root Welcome t
如何使用 Swift3 在 UILabel 末尾添加 UIButton,就像阅读更多一样? 我已经使用 Storyboard创建了 UILabel 和 UIButton。 我不想使用任何 Pod
所以我即将将我的服务器迁移到新机器。我在新机器上安装较新版本的mysql 5.7.13,而旧版本的mysql 5.5.13。有一个查询不像以前那样工作与之前的服务器数据相同 select * fr
在 Andrei Alexandrescu 关于错误处理的演讲中: 见 C++ and Beyond 2012: Andrei Alexandrescu - Systematic Error Hand
我指的是本教程 http://www.tldp.org/LDP/abs/html/sha-bang.html#AEN226在 bash 上进一步提高我的知识,但我似乎无法使用网站中提到的脚本命令删除我
我正在尝试为允许 string 的对象编写类型s 作为键和 function s 带有任意数量的参数,后跟作为值的回调。 // non working dummy code type MyObject
我刚刚用 c 语言编写了一个使用命令行参数的程序,我试图打印第一个参数。当我使用以下命令执行程序时 ./a.out $23 并尝试使用下面的代码打印第一个参数 printf("%s", argv[1]
申请 UILongPressGestureRecongnizer一方面, 检查下面的代码以供引用.. @interface ViewController () { UIRotationGes
我正在使用 MPI 和 C 进行编程,并且使用根等级从文件中读取数据,然后将其分发到其余等级。我的 MPI_Scatter 工作正常,我打印出这些值以确保它们是正确的(而且确实如此)。我的问题是,分配
每当“Tskb”作为单个单词出现时,我必须使用 Java 正则表达式将“Tskb”转换为“TsKB”。我写了下面不起作用的代码。 public class TestBGR { priv
我有下面的代码(注意,我稍后会在循环中添加更多代码,但我需要它先工作): var calls_on = true; function hunt(max, ext, duration){ if(
在我的非根 VC 类中,我有以下 doLogout 方法: - (void)doLogout { SDPAppDelegate *app = [self getAppDelegate];
我的数据库中有很多地址。示例:1199 国道 218 我想选择起始数字,后跟第一个单词,并忽略地址的其余部分。所以我想通过调用这个来检索地址:1199 State 感谢您的帮助 最佳答案 如果您运行的
我正在创建一些客户端套接字软件来从服务器读取事件。 (例如,流媒体股票报价)。 问题: .read(b); 立即返回值-1。这会导致无限循环,手机会变得很热。此外,对 s.isConnected()、
我这里有这段代码: var fields = row.split(/regex goes here/); 我想在每次出现时分割行 | 但不是 *| 如何编写这个简单的正则表达式。我认为有一个不是字符,
我是一名优秀的程序员,十分优秀!