Python返回的字符串同时是str和unicode typ

2024-05-31 23:59:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我们有一个可以由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时,这怎么可能呢?在

谢谢你


Tags: 代码from版本脚本log应用程序getnet
2条回答

IronPython在strunicode之间没有区别,如果您习惯了cpython2语义,这可能会非常混乱。实际上,basestring和{}都是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)的行为更像python3,但有一点令人恼火的事实是,如果您想基于类型进行解码/编码,就不能区分unicode和{}(由于它仍然是Python 2,因此也没有bytes)。在

在IronPython中,str类型和unicode类型是同一个对象。.NET字符串类型为unicode。在

相关问题 更多 >