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

标签 python .net 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)))

两条日志行都将返回 True ??

stringToPlay 值为“Ћирилица”。

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

谢谢

最佳答案

IronPython 在 strunicode 之间没有区别,如果您习惯了 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'>

关于字符串,IronPython (2.X) 的行为更像 Python 3,有一个有点烦人的事实,即如果您愿意,您无法区分 unicodestr根据类型解码/编码(因为它仍然是 Python 2,所以也没有 bytes)。

关于Python 返回的字符串既是 str 又是 unicode 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39345916/

相关文章:

Python:getsize 无法从 itunes xml 中找到文件位置

windows - 将非 unicode 库与 unicode 构建的应用程序一起使用的含义

python - 是否可以在 pdfquery 中使用正则表达式?

python - 密码重置功能 Django-Registration-Redux 应用程序

c# - 编写 XML 最快的方法是什么

.net - .NET Framework 是否有发布任务? (不是 .NET Core)

python:将self传递给类方法内的嵌套函数是否合法?

python - 更改列表中的特定索引

c# - 是否有低级网络库来嗅探和更改网络流量?

具有不同编码的 Rd 文件