簡體   English   中英

Python返回string是str和unicode類型

[英]Python returns string is both str and unicode type

我們有一個可以通過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 )。

在IronPython中, str類型和unicode類型是同一個對象。 .NET字符串類型是unicode。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM