在ruby中隐藏系统命令的结果有多容易?例如,我的一些脚本运行
system "curl ..."
Run Code Online (Sandbox Code Playgroud)
我不希望看到下载的结果.
如果我做的事情
>>> x = int(1,2,3,4,5)
Run Code Online (Sandbox Code Playgroud)
我立即得到一个致命的错误(如果它是在一个预先编写的脚本中就会结束程序执行)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: int() takes at most 2 arguments (5 given)
Run Code Online (Sandbox Code Playgroud)
并且x仍未定义:
>>> x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
Run Code Online (Sandbox Code Playgroud)
我将如何在Python的C API中实现它?我找到了一些文档,但我不确定我是否知道如何正确使用它.
这是我一直在尝试的:
打印:
if(something) {
PyErr_SetString(PyExc_TypeError, "Oh no!");
PyErr_Print();
}
Run Code Online (Sandbox Code Playgroud)
遗憾的是,这只会打印异常并继续执行程序.另外,如果我理解正确 - PyErr_Print()从某种队列中删除异常,因此Python认为它已被处理.这就是它的样子:
>>> import awesomemod
>>> x = awesomemod.thing()
TypeError: Oh no!
>>> x # x is …Run Code Online (Sandbox Code Playgroud)以下列方式很容易在0和1之间"切换":
int i = 0;
i = (++i) % 2; // i = 1
i = (++i) % 2; // i = 0
Run Code Online (Sandbox Code Playgroud)
同样,我发现可以在3到5之间"切换":
int i = 3;
i = (((i * 2) - 1) % 3) + 3; // i = 5
i = (((i * 2) - 1) % 3) + 3; // i = 3
Run Code Online (Sandbox Code Playgroud)
虽然这感觉很麻烦,但我正在寻找一种更简洁的方法来做到这一点.可以简化吗?如果是这样,怎么样?顺便说一下,我实际上是在用这个东西.
我正在为AP计算机科学考试做准备,我对文本对Integer课程的讨论感到困惑.
文本定义Integer的compareTo,如下所示:
int compareTo(Object other)
Run Code Online (Sandbox Code Playgroud)
从一开始我就知道它会有一个Object参数,因为我认为这是它可以实现的唯一方法Comparable.但是,我最终查看了IntegerEclipse 中的类,我发现它使用了泛型Comparable,因此compareTo使用Integer参数定义.
这是我书中的错误吗?这使我相信,这是铸造Object成Integer的方法,这是错误的(并就考试的差别).我在这里错过了什么吗?