Python返回的字符串是str和unicode类型

Python返回的字符串是str和unicode类型,python,.net,unicode,ironpython,Python,.net,Unicode,Ironpython,我们有一个可以由IronPython定制的.NET应用程序(版本2.7.5) 以下是脚本代码: 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.Writ

我们有一个可以由IronPython定制的.NET应用程序(版本2.7.5)

以下是脚本代码:

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
类型是同一个对象。.NET字符串类型是unicode。

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'>
.NET4.0.30319.42000(32位)上的IronPython 2.7.4(2.7.0.40) 有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。 >>>统一码 >>>基线
关于字符串,IronPython(2.X)的行为更像Python3,有一个稍微令人恼火的事实,即如果您想基于类型进行解码/编码(由于它仍然是Python2,因此也没有
字节
),您无法区分
unicode
str

类型的结果是什么(stringToPlay)
?你的
.NET字符串
System.string
?那么Unify和IronPython有什么问题吗,就像CPython 2.*那样,或者只是你列出的那个细节?你是说Unicode,不是Unify,对吗?Python2和Unicode的“问题”是字符串最初只是编码的字节字符串(ASCII、Latin1、UTF-8或其他)。
unicode
类型(表示编码独立的字符串)的引入会导致混淆,因为现在有两种具有复杂关系的字符串类型。特别是调用
encode
/
decode
而不管类型是什么都是有问题的(例如,对已经编码的字符串进行编码、对unicode字符串进行解码等)经验法则:像IronPython中的unicode字符串一样处理所有字符串。切勿对字符串调用
decode
encode
,除非您正在执行IO。那你应该没有问题。(这实际上是模仿Python3字符串处理风格)。