Return `undefined` from Python function with PyV8?

烈酒焚心 提交于 2019-12-12 17:32:05

问题


I'm using PyV8 and I'd like to call a javascript function with undefined. It seems that evaluating both undefined and null return Python's None value:

>>> evaljs("undefined")
None
>>> evaljs("null")
None

The problem, of course, is that they're not the same in javascript:

>>> evaljs("undefined === null")
False
False
>>> evaljs("undefined") == evaljs("null")
None
None
True

Is there any nice way to go about doing this? I'd actually like to write a Python function, callable from javascript, which returns undefined in certain cases and null in other cases.

EDIT for SSCCE-ness:

import PyV8

context = PyV8.JSContext()

def evaljs(s):
    with context:
        res = context.eval(s)
        print res
        return res

回答1:


As you can see here: null object in Python?, there is no null or undefined in Python, just None... so you would only be able to represent undefined and null from JavaScript as None in Python. JavaScript makes a distinction between the two for its own reasons and, as far as I can tell from my own experiences, "undefined" is fairly unique to JavaScript.

The only thing I can think of would be to get down into the source and see how undefined and null are being tracked in the JavaScript v8 engine and trying to have that object return when you evaluate JavaScript statements with null or undefined results... but that's quite the daunting task.




回答2:


The answer is: in the current version there is no way to do this.



来源:https://stackoverflow.com/questions/16995108/return-undefined-from-python-function-with-pyv8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!