在导入中转换导入
transcrypt import in import
使用 Transcrypt 进行 python 到 javascript 编译 我有 2 个模块需要彼此。例如:
myTest.py:
import myTest2
def test():
myTest2.test()
someConstant = 1
和myTest2.py:
import myTest
def test():
console.log(myTest.someConstant)
编译为 javascript 并调用 myTest.test()
后,我得到一个 RangeError:超出最大调用堆栈大小。
我怎样才能避免这种情况,但要保留 2 个相互使用的模块?
提前致谢。
尝试在需要时从 myTest
导入。
在mytest2.py
def test():
from myTest import someConstant
console.log(someConstant)
在 Transcrypt 中导入是在编译时而不是运行时解析的,因为编译器必须知道要在生成的 JavaScript 中包含哪些模块。此外,导入解析发生在一次通过中。解决方案在单次通过中发生这一事实意味着不支持相互(或一般循环)导入。
因此,如果您有两个模块需要彼此的某些东西,那么解决方法是将某些东西分解并放入第三个模块中,由两个模块导入。
解析发生在编译时的事实也意味着运行时条件导入没有意义,使用 'if'。对于条件导入,使用 __pragma__ ('ifdef', ...) 它会在编译时完成工作。
此类限制的解释如下:
http://www.transcrypt.org/docs/html/special_facilities.html#transcrypt-s-module-mechanism
使用 Transcrypt 进行 python 到 javascript 编译 我有 2 个模块需要彼此。例如:
myTest.py:
import myTest2
def test():
myTest2.test()
someConstant = 1
和myTest2.py:
import myTest
def test():
console.log(myTest.someConstant)
编译为 javascript 并调用 myTest.test()
后,我得到一个 RangeError:超出最大调用堆栈大小。
我怎样才能避免这种情况,但要保留 2 个相互使用的模块?
提前致谢。
尝试在需要时从 myTest
导入。
在mytest2.py
def test():
from myTest import someConstant
console.log(someConstant)
在 Transcrypt 中导入是在编译时而不是运行时解析的,因为编译器必须知道要在生成的 JavaScript 中包含哪些模块。此外,导入解析发生在一次通过中。解决方案在单次通过中发生这一事实意味着不支持相互(或一般循环)导入。
因此,如果您有两个模块需要彼此的某些东西,那么解决方法是将某些东西分解并放入第三个模块中,由两个模块导入。
解析发生在编译时的事实也意味着运行时条件导入没有意义,使用 'if'。对于条件导入,使用 __pragma__ ('ifdef', ...) 它会在编译时完成工作。
此类限制的解释如下:
http://www.transcrypt.org/docs/html/special_facilities.html#transcrypt-s-module-mechanism