简体   繁体   English

使用PyV8从Python函数返回`undefined`吗?

[英]Return `undefined` from Python function with PyV8?

I'm using PyV8 and I'd like to call a javascript function with undefined . 我正在使用PyV8,并且想使用undefined调用javascript函数。 It seems that evaluating both undefined and null return Python's None value: 似乎同时评估undefinednull返回Python的None值:

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

The problem, of course, is that they're not the same in javascript: 当然,问题在于它们在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. 我实际上想编写一个可从javascript调用的Python函数,在某些情况下返回undefined ,在其他情况下返回null

EDIT for SSCCE -ness: 编辑SSCCE -ness

import PyV8

context = PyV8.JSContext()

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

As you can see here: null object in Python? 如您所见: 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. ,Python中没有nullundefined ,只有None ...因此您只能将JavaScript中undefinednull表示为Python中的None 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. JavaScript出于其自身的原因在两者之间进行了区分,并且据我的经验所知,“未定义”对于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. 我唯一能想到的就是深入研究源代码,并查看在JavaScript v8引擎中如何对undefined和null进行跟踪,并在评估具有null或undefined结果的JavaScript语句时尝试返回该对象...但是这是一项艰巨的任务。

答案是:在当前版本中,无法执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM