我们有一个可以通过 IronPython(版本 2.7.5)自定义的 .NET 应用
脚本代码如下:
stringToPlay = # get it from our .NET app interface toward python here. The method returns .NET string
Log.Write("isinstance(stringToPlay, unicode): ", str(isinstance(stringToPlay, unicode)))
Log.Write("isinstance(stringToPlay, str): ", str(isinstance(stringToPlay, str)))
两条日志行都将返回 True ??
stringToPlay 值为“Ћирилица”。
当 str 和 unicode 应该是两个都继承自 basestring 的独立类时,这怎么可能?
谢谢
最佳答案
IronPython 在 str
和 unicode
之间没有区别,如果您习惯了 CPython 2 语义,这可能会非常困惑。事实上,basestring
和unicode
都是str
的别名。
IronPython 2.7.4 (2.7.0.40) on .NET 4.0.30319.42000 (32-bit)
Type "help", "copyright", "credits" or "license" for more information.
>>> unicode
<type 'str'>
>>> basestring
<type 'str'>
关于字符串,IronPython (2.X) 的行为更像 Python 3,有一个有点烦人的事实,即如果您愿意,您无法区分 unicode
和 str
根据类型解码/编码(因为它仍然是 Python 2,所以也没有 bytes
)。
关于Python 返回的字符串既是 str 又是 unicode 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39345916/