否则内联?
Inline if else?
这是部分代码。我需要了解这段代码的作用:
if instruction == "JOINT" or instruction == "ROOT":
parent = joint_stack[-1] if instruction == "JOINT" else None
joint = BvhJoint(words[1], parent)
self.joints[joint.name] = joint
如何理解这一行的特殊性->
parent = joint_stack[-1] if instruction == "JOINT" else None
这段代码是否等同于JS代码:
if (instruction == "JOINT") {
parent = joint_stack[-1];
} else {
}
?
等效的 JS 代码为:
if (instruction == "JOINT") {
parent = joint_stack[-1];
} else {
parent = null;
}
或更惯用的说法:
parent = instruction == "JOINT" ? joint_stack[-1] : null;
Python中的这个三元表达式:
joint_stack[-1] if instruction == "JOINT" else None
与JS三元表达式相同:
instruction == "JOINT" ? joint_stack[-1] : null
parent = joint_stack[-1] if instruction == "JOINT" else None
这将检查“指令”是否等于“JOINT”,如果是,joint_stack 数组的最后一个值将分配给父级,否则None
等效的 JS 是
const parent = (instruction === "JOINT")?joint_stack[-1]:null;
条件的快捷方式
表达式已添加到 Python 2.5. 参见 Does Python have a ternary conditional operator?
这是部分代码。我需要了解这段代码的作用:
if instruction == "JOINT" or instruction == "ROOT":
parent = joint_stack[-1] if instruction == "JOINT" else None
joint = BvhJoint(words[1], parent)
self.joints[joint.name] = joint
如何理解这一行的特殊性->
parent = joint_stack[-1] if instruction == "JOINT" else None
这段代码是否等同于JS代码:
if (instruction == "JOINT") {
parent = joint_stack[-1];
} else {
}
?
等效的 JS 代码为:
if (instruction == "JOINT") {
parent = joint_stack[-1];
} else {
parent = null;
}
或更惯用的说法:
parent = instruction == "JOINT" ? joint_stack[-1] : null;
Python中的这个三元表达式:
joint_stack[-1] if instruction == "JOINT" else None
与JS三元表达式相同:
instruction == "JOINT" ? joint_stack[-1] : null
parent = joint_stack[-1] if instruction == "JOINT" else None
这将检查“指令”是否等于“JOINT”,如果是,joint_stack 数组的最后一个值将分配给父级,否则None
等效的 JS 是
const parent = (instruction === "JOINT")?joint_stack[-1]:null;
条件的快捷方式 表达式已添加到 Python 2.5. 参见 Does Python have a ternary conditional operator?