Python返回string是str和unicode类型

Срб*_*рба 2 .net python unicode ironpython

我们有一个可以通过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)))
Run Code Online (Sandbox Code Playgroud)

两条日志行都会返回True?

stringToPlay值为"Ћирилица".

当str和unicode应该是两个独立的类都继承自basestring时,这怎么可能?

谢谢

cod*_*kel 6

IronPython在str和之间没有区别unicode,如果您习惯于CPython 2语义,这可能会非常混乱.事实上,basestringunicode都是别名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'>
Run Code Online (Sandbox Code Playgroud)

关于字符串,IronPython(2.X)的行为更像Python 3,有一个令人讨厌的事实,你无法区分unicode,str如果你想根据类型解码/编码(因为它仍然是Python 2,有也没有bytes).