Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 38953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:47:20+00:00 2026-05-10T14:47:20+00:00

I start by creating a string variable with some non-ascii utf-8 encoded data on

  • 0

I start by creating a string variable with some non-ascii utf-8 encoded data on it:

>>> text = 'á' >>> text '\xc3\xa1' >>> text.decode('utf-8') u'\xe1' 

Using unicode() on it raises errors…

>>> unicode(text) Traceback (most recent call last):   File '<stdin>', line 1, in <module> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0:                      ordinal not in range(128) 

…but if I know the encoding I can use it as second parameter:

>>> unicode(text, 'utf-8') u'\xe1' >>> unicode(text, 'utf-8') == text.decode('utf-8') True 

Now if I have a class that returns this text in the __str__() method:

>>> class ReturnsEncoded(object): ...     def __str__(self): ...         return text ...  >>> r = ReturnsEncoded() >>> str(r) '\xc3\xa1' 

unicode(r) seems to use str() on it, since it raises the same error as unicode(text) above:

>>> unicode(r) Traceback (most recent call last):   File '<stdin>', line 1, in <module> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0:                      ordinal not in range(128) 

Until now everything is as planned!

But as no one would ever expect, unicode(r, 'utf-8') won’t even try:

>>> unicode(r, 'utf-8') Traceback (most recent call last):   File '<stdin>', line 1, in <module> TypeError: coercing to Unicode: need string or buffer, ReturnsEncoded found 

Why? Why this inconsistent behavior? Is it a bug? is it intended? Very awkward.

  • 1 1 Answer
  • 6 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T14:47:20+00:00Added an answer on May 10, 2026 at 2:47 pm

    The behaviour does seem confusing, but intensional. I reproduce here the entirety of the unicode documentation from the Python Built-In Functions documentation (for version 2.5.2, as I write this):

    unicode([object[, encoding [, errors]]])

    Return the Unicode string version of object using one of the following modes:

    If encoding and/or errors are given, unicode() will decode the object which can either be an 8-bit string or a character buffer using the codec for encoding. The encoding parameter is a string giving the name of an encoding; if the encoding is not known, LookupError is raised. Error handling is done according to errors; this specifies the treatment of characters which are invalid in the input encoding. If errors is ‘strict’ (the default), a ValueError is raised on errors, while a value of ‘ignore’ causes errors to be silently ignored, and a value of ‘replace’ causes the official Unicode replacement character, U+FFFD, to be used to replace input characters which cannot be decoded. See also the codecs module.

    If no optional parameters are given, unicode() will mimic the behaviour of str() except that it returns Unicode strings instead of 8-bit strings. More precisely, if object is a Unicode string or subclass it will return that Unicode string without any additional decoding applied.

    For objects which provide a __unicode__() method, it will call this method without arguments to create a Unicode string. For all other objects, the 8-bit string version or representation is requested and then converted to a Unicode string using the codec for the default encoding in ‘strict’ mode.

    New in version 2.0. Changed in version 2.2: Support for __unicode__() added.

    So, when you call unicode(r, 'utf-8'), it requires an 8-bit string or a character buffer as the first argument, so it coerces your object using the __str__() method, and attempts to decode that using the utf-8 codec. Without the utf-8, the unicode() function looks for a for a __unicode__() method on your object, and not finding it, calls the __str__() method, as you suggested, attempting to use the default codec to convert to unicode.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a simple questions DB, here are some details to start: 1st off,
On application start I'm creating Config object (Singleton). How could I make Config object
I'm creating a new shorcut within and update of my program on the Start
I am creating dynamic menus and submenus in php.... Can't know where to start....
I'm creating a game. The levels are relatively short, and at the start of
I am creating an ajax login and it used to work but for some
So it seems I'm misunderstanding some basic stuff about Python. Passing an instance variable
I'm creating a mock data source that I want to be able to pass
I have a couple of queries out here before I start creating my custom
I'm creating a stat editor for some objects within a game world. Rather than

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.