我们有一个可以通过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继承的独立类时,这怎么可能?
谢谢
答案 0 :(得分:6)
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
)。
答案 1 :(得分:4)
在IronPython中,str
类型和unicode
类型是同一个对象。 .NET字符串类型是unicode。