Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python returns string is both str and unicode type

We have a .NET app that can be customized by IronPython (version 2.7.5)

Here is the script code:

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)))

Both log lines will return True ??

stringToPlay value is "Ћирилица" .

How is this possible when str and unicode should be two separate classes both inheriting from basestring?

Thank you

like image 965
Срба Avatar asked Jul 28 '26 12:07

Срба


1 Answers

IronPython makes no difference between str and unicode, which can be very confusing if you are used to the CPython 2 semantics. In fact, basestring and unicode are both aliases for 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'>

Regarding strings, IronPython (2.X) behaves more like Python 3, with the slightly annoying fact that you can't distinguish between unicode and str if you want to decode / encode based on the type (and since it is still Python 2, there is also no bytes).

like image 131
code_onkel Avatar answered Jul 30 '26 03:07

code_onkel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!