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 8191013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:49:10+00:00 2026-06-07T03:49:10+00:00

I was given to understand that calling print obj would call obj.__str__() which would

  • 0

I was given to understand that calling print obj would call obj.__str__() which would in turn return a string to print to the console. Now I head a problem with Unicode where I could not print any non-ascii characters. I got the typical “ascii out of range” stuff.

While experimenting the following worked:

print obj.__str__()
print obj.__repr__()

With both functions doing exactly the same (__str__() just returns self.__repr__()). What did not work:

print obj

The problem occured only with using a character out of ascii range. The final solution was to to the following in __str__():

return self.__repr__().encode(sys.stdout.encoding)

Now it works for all parts. My question now is: Where is the difference? Why does it work now? I get if nothing worked, why this works now. But why does only the top part work, not the bottom.

OS is Windows 7 x64 with a default Windows command prompt. Also the encoding is reported to be cp850. This is more of a general question to understand python. My problem is already solved, but I am not 100% happy, mostly because now calling str(obj) will yield a string that is not encoded in the way I wanted it.

# -*- coding: utf-8 -*- 
class Sample(object):

    def __init__(self):
        self.name = u"üé"

    def __repr__(self):
        return self.name

    def __str__(self):
        return self.name

obj = Sample()
print obj.__str__(), obj.__repr__(), obj

Remove the last obj and it works. Keep it and it crashes with

UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
  • 1 1 Answer
  • 0 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. Editorial Team
    Editorial Team
    2026-06-07T03:49:12+00:00Added an answer on June 7, 2026 at 3:49 am

    My guess is that print does something like the following for an object obj it’s meant to print:

    1. Checks if the obj is a unicode. If so, encodes it to sys.stdout.encoding and prints.
    2. Checks if the obj is a str. If so, prints it directly.
    3. If obj is anything else, calls str(obj) and prints that.

    Step 1. is why print obj.__str__() works in your case.

    Now, what str(obj) does is:

    1. Call obj.__str__().
    2. If the result is a str, return it
    3. If the result is a unicode, encodes it to "ascii" and return that
    4. Otherwise, something mostly useless.

    Calling obj.__str__() directly skips steps 2-3, which is why you don’t get the encoding failure.

    The problem isn’t caused by how print works, it’s caused by how str() works. str() ignores sys.stdout.encoding. Since it doesn’t know what you want to do with the resulting string, the default encoding it uses can be considered arbitrary; ascii is as good or bad a choice as any.

    To prevent this bug, make sure you return a str from __str__() as the documentation tells you to do. A pattern you could use for Python 2.x might be:

    class Foo():
        def __unicode__(self):
            return u'whatever'
        def __str__(self):
            return unicode(self).encode(sys.stdout.encoding)
    

    (If you’re sure you don’t need the str() representation for anything but printing to the console.)

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

Sidebar

Related Questions

I understand that CoCreateInstance finds the COM server for the given class id, creates
I want to understand how a C++ program that was given to me works,
Possible Duplicate: Sizeof array passed as parameter Given the function below I understand that
I'm trying to understand how one should design middlewares for EWGI compatibility. Given that
I understand that the elements of a python set are not ordered. Calling the
Given the Ruby code line = first_name=mickey;last_name=mouse;country=usa record = Hash[*line.split(/=|;/)] I understand everything in
I'm trying to understand how parallelism might work using PLINQ, given delayed execution. Here
I understand that execve() and family require the first argument of its argument array
I had been programming under the assumption that, when calling a method in C#
Given a class like this one: [Serializable] public class MyClass { string name; string

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.