Should I use Unicode string by default?

前端 未结 6 827
星月不相逢
星月不相逢 2021-02-07 03:38

Is it considered as a good practice to pick Unicode string over regular string when coding in Python? I mainly work on the Windows platform, where most of the string types are U

相关标签:
6条回答
  • 2021-02-07 04:00

    It can be tricky to consistently use unicode strings in Python 2.x - be it because somebody inadvertently uses the more natural str(blah) where they meant unicode(blah), forgetting the u prefix on string literals, third-party module incompatibilities - whatever. So in Python 2.x, use unicode only if you have to, and are prepared to provide good unit test coverage.

    If you have the option of using Python 3.x however, you don't need to care - strings will be unicode with no extra effort.

    0 讨论(0)
  • 2021-02-07 04:01

    Additional to Mihails comment I would say: Use Unicode, since it is the future. In Python 3.0, Non-Unicode will be gone, and as much I know, all the "U"-Prefixes will make trouble, since they are also gone.

    0 讨论(0)
  • 2021-02-07 04:11

    Yes, use unicode.

    Some hints:

    1. When doing input output in any sort of binary format, decode directly after reading and encode directly before writing, so that you never need to mix strings and unicode. Because mixing that tends to lead to UnicodeEncodeDecodeErrors sooner or later.

    2. [Forget about this one, my explanations just made it even more confusing. It's only an issue when porting to Python 3, you can care about it then.]

    3. Common Python newbie errors with Unicode (not saying you are a newbie, but this may be read by newbies): Don't confuse encode and decode. Remember, UTF-8 is an ENcoding, so you ENcode Unicode to UTF-8 and DEcode from it.

    4. Do not fall into the temptation of setting the default encoding in Python (by setdefaultencoding in sitecustomize.py or similar) to whatever you use most. That is just going to give you problems if you reinstall or move to another computer or suddenly need to use another encoding. Be explicit.

    5. Remember, not all of Python 2s standard library accepts unicode. If you feed a method unicode and it doesn't work, but it should, try feeding it ascii and see. Examples: urllib.urlopen(), which fails with unhelpful errors if you give it a unicode object instead of a string.

    Hm. That's all I can think of now!

    0 讨论(0)
  • 2021-02-07 04:13

    From my practice -- use unicode.

    At beginning of one project we used usuall strings, however our project was growing, we were implementing new features and using new third-party libraries. In that mess with non-unicode/unicode string some functions started failing. We started spending time localizing this problems and fixing them. However, some third-party modules doesn't supported unicode and started failing after we switched to it (but this is rather exclusion than a rule).

    Also I have some experience when we needed to rewrite some third party modules(e.g. SendKeys) cause they were not supporting unicode. If it was done in unicode from beginning it will be better :)

    So I think today we should use unicode.

    P.S. All that mess upwards is only my hamble opinion :)

    0 讨论(0)
  • 2021-02-07 04:15

    As you ask this question, I suppose you are using Python 2.x.

    Python 3.0 changed quite a lot in string representation, and all text now is unicode.
    I would go for unicode in any new project - in a way compatible with the switch to Python 3.0 (see details).

    0 讨论(0)
  • 2021-02-07 04:24

    If you are dealing with severely constrained memory or disk space, use ASCII strings. In this case, you should additionally write your software in C or something even more compact :)

    0 讨论(0)
提交回复
热议问题