评价'not (False and True)'
Evaluation of 'not (False and True)'
我遇到了这个例子("Hacking Secret Ciphers with Python",p135),我很困惑:
not (False and True)
该示例指出它的计算结果为 False
,但我不明白为什么。如果括号修改了优先级,那么我们肯定正在评估:
not (False)
计算结果为 True
,不是吗?还是我遗漏了什么?
// output functions are configurable. This one just appends some text
// to a pre element.
function outf(text) {
var mypre = document.getElementById("output");
mypre.innerHTML = mypre.innerHTML + text;
}
function builtinRead(x) {
if (Sk.builtinFiles === undefined || Sk.builtinFiles["files"][x] === undefined)
throw "File not found: '" + x + "'";
return Sk.builtinFiles["files"][x];
}
// Here's everything you need to run a python program in skulpt
// grab the code from your textarea
// get a reference to your pre element for output
// configure the output function
// call Sk.importMainWithBody()
function runit() {
var prog = document.getElementById("yourcode").value;
var mypre = document.getElementById("output");
mypre.innerHTML = '';
Sk.pre = "output";
Sk.configure({
output: outf,
read: builtinRead
});
(Sk.TurtleGraphics || (Sk.TurtleGraphics = {})).target = 'mycanvas';
var myPromise = Sk.misceval.asyncToPromise(function() {
return Sk.importMainWithBody("<stdin>", false, prog, true);
});
myPromise.then(function(mod) {
console.log('success');
},
function(err) {
console.log(err.toString());
});
}
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js" type="text/javascript"></script>
<script src="http://www.skulpt.org/static/skulpt.min.js" type="text/javascript"></script>
<script src="http://www.skulpt.org/static/skulpt-stdlib.js" type="text/javascript"></script>
<h3>Try This</h3>
<form>
<textarea id="yourcode" cols="40" rows="10">print(not (False and True))
</textarea>
<br />
<button type="button" onclick="runit()">Run</button>
</form>
<pre id="output"></pre>
<!-- If you want turtle graphics include a canvas -->
<div id="mycanvas"></div>
not (False and True)
肯定是 True
,如果您在 Python REPL 中键入表达式,您会看到这一点。谁声称它是 False
?
这实际上涵盖在the errata:
Page 135, the interactive shell should look like this:
>>> not False and False # not False evaluates first
True
>>> not (False and False) # (False and False) evaluates first
False
(Thanks to Omer Chohan)
已在 the online version of the chapter 中更正,但 PDF 中没有。
我遇到了这个例子("Hacking Secret Ciphers with Python",p135),我很困惑:
not (False and True)
该示例指出它的计算结果为 False
,但我不明白为什么。如果括号修改了优先级,那么我们肯定正在评估:
not (False)
计算结果为 True
,不是吗?还是我遗漏了什么?
// output functions are configurable. This one just appends some text
// to a pre element.
function outf(text) {
var mypre = document.getElementById("output");
mypre.innerHTML = mypre.innerHTML + text;
}
function builtinRead(x) {
if (Sk.builtinFiles === undefined || Sk.builtinFiles["files"][x] === undefined)
throw "File not found: '" + x + "'";
return Sk.builtinFiles["files"][x];
}
// Here's everything you need to run a python program in skulpt
// grab the code from your textarea
// get a reference to your pre element for output
// configure the output function
// call Sk.importMainWithBody()
function runit() {
var prog = document.getElementById("yourcode").value;
var mypre = document.getElementById("output");
mypre.innerHTML = '';
Sk.pre = "output";
Sk.configure({
output: outf,
read: builtinRead
});
(Sk.TurtleGraphics || (Sk.TurtleGraphics = {})).target = 'mycanvas';
var myPromise = Sk.misceval.asyncToPromise(function() {
return Sk.importMainWithBody("<stdin>", false, prog, true);
});
myPromise.then(function(mod) {
console.log('success');
},
function(err) {
console.log(err.toString());
});
}
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js" type="text/javascript"></script>
<script src="http://www.skulpt.org/static/skulpt.min.js" type="text/javascript"></script>
<script src="http://www.skulpt.org/static/skulpt-stdlib.js" type="text/javascript"></script>
<h3>Try This</h3>
<form>
<textarea id="yourcode" cols="40" rows="10">print(not (False and True))
</textarea>
<br />
<button type="button" onclick="runit()">Run</button>
</form>
<pre id="output"></pre>
<!-- If you want turtle graphics include a canvas -->
<div id="mycanvas"></div>
not (False and True)
肯定是 True
,如果您在 Python REPL 中键入表达式,您会看到这一点。谁声称它是 False
?
这实际上涵盖在the errata:
Page 135, the interactive shell should look like this:
>>> not False and False # not False evaluates first True >>> not (False and False) # (False and False) evaluates first False
(Thanks to Omer Chohan)
已在 the online version of the chapter 中更正,但 PDF 中没有。