这是python代码i以 transcrypt -b -n -m (版本3.6.84(传输的代码:
>def test_1():
console.log('before assert')
assert False, "False fails"
console.log('we should not see that if assert fails as expected')
def test_2():
console.log('before assert')
try:
assert False, "False fails"
except AssertionError as exception:
console.log('we should see that since we catch the assertion error')
console.log('after assert')
当我在浏览器控制台中运行test_1/test_2时,我会得到一个奇怪的行为:
> mymodule.test_1()
before assert
we should not see that if assert fails as expected
<- undefined
> mymodule.test_2()
before assert
after assert
<- undefined
为什么断言没有例外?
回答问题482:
您需要-DA开关来激活断言:
transcrypt -b -n -m -da
我已经测试了:
def test_1():
console.log('before assert')
assert False, "False fails"
console.log('we should not see that if assert fails as expected')
def test_2():
console.log('before assert')
try:
assert False, "False fails"
except AssertionError as exception:
console.log('we should see that since we catch the assertion error')
console.log('after assert')
try:
test_1()
except AssertionError as exception:
console.log('we should see this')
test_2()
它打印:
before assert
we should see this
before assert
we should see that since we catch the assertion error
after assert
thressCrypt中的许多东西都是可选的,以防止生成的JavaScript膨胀。某些功能由命令行开关控制:
http://www.transcrypt.org/docs/html/installation_use.html#available-command-line-switches
某些功能(也(由Pragma的(编译器指令(控制:
http://www.transcrypt.org/docs/html/special_facilities.html#the-pragma-mechanism